Monday, November 20, 2006

Oracle: Generating Random Numbers

In generating a large set of test data for report development, I had needed to populate a columns data with random numbers between a given range. Fortunately, Oracle provides this in the form of its DBMS_RANDOM package.

For my case, I needed to set a foreign keys value to anything within a range of values in another table. I know that my range in this other table is 1-100, so to update my table with those values, I would run something like:

Update mytable set myfield = trunc(DBMS_RANDOM.VALUE(1, 100))

The trunc became necessary since Oracle was spitting out values with decimals.

No comments: