|
Q and A
Asked and Answered
How do you generate random numbers in DB2?
Robert Catterall responds:
If you want to generate a random number to populate a column in a DB2 table, you have a number of options, divided into two broad categories: generate the value outside of DB2 or use a DB2 facility to generate the value.
To generate the value outside of DB2, here are some approaches:
- Obtain a timestamp value and transpose some low-order digits to the front of the value.
- Obtain two timestamp values and choose selected positional digits from each to generate a single random value.
- Use a random number generation routine (which might be part of a program library you already have in-house).
- Use a hashing algorithm. I believe such a routine, called MD5, is often used in a Java development environment. (For more information, enter "MD5" in an Internet search engine).
If you want to use a DB2 facility, check out the built-in DB2 scalar function RAND. (See the SQL Reference for more information).
See a
complete archive of reader/author Q&As
.
|