PL/SQL

Using the Oracle DBMS_RANDOM Package To Generate Random Numbers

Have you ever needed to generate a random number within your Oracle PL/SQL code? I recently had a need to just this. My requirement was to generate a random 6 digit pin number for users to login to our web systems with.

I found that the dbms_random package does exactly what I need, and is extremely simple to use.

My first test of this was just to run the statement:

select dbms_random.value
from dual

This will return a random number, greater than or equal to 0 and less than 1, with decimal 38 digits. When I ran this command my result was:
0.622324263489056

The DBMS_RANDOM.VALUE function can also take 2 arguments, the low number and the high number. By using these two arguments you can get a random Oracle number x, where x is greater than or equal to the low number and less than the high number.

Syndicate content
v2.0