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 .

CAREER CENTER
Ready to take that job and shove it?
SEARCH JOBS
RECENT JOB POSTINGS
CAREER NEWS
10 Search Engines You Don't Know About
Go beyond Google and get vertical. These specialized search sites will help you find the business information you need -- fast.

Subscribe to the new digital version of IBM Database Magazine
New Digital Version

Sponsored links:



Subscribe to the IBM Database Magazine Newsletter

Email Address *
First Name
Last Name
HTML Preference
HTML Text
 

Fields with * are required.

 




Visit these other IBM and TechWeb Partner Sites: :
Maximizing ROI Through Business Process Management (BPM) and Service-Oriented Architecture (SOA)
Internet Evolution – The Macrosite for News, Analysis, & Opinion About the Future of the Internet
Business Innovation – Technology Strategies and Solutions for Driving Business Success


:: IBM Database Magazine ::