6 DBMS_RANDOM
The DBMS_RANDOM package provides a built-in random number generator.
This chapter contains the following topics:
-
-
Operational notes
-
Note:
DBMS_RANDOM is not intended for cryptography.
Using DBMS_RANDOM
Operational Notes
-
The
RANDOMfunction produces integers in the range [-2^^31, 2^^31). -
The
VALUEfunction produces numbers in the range [0,1) with 38 digits of precision.
DBMS_RANDOM can be explicitly initialized but does not require initialization before a call to the random number generator. It automatically initializes with the date, user ID, and process ID if no explicit initialization is performed.
If this package is seeded twice with the same seed, then accessed in the same way, it produces the same result in both cases.
In some cases, such as when testing, you may want the sequence of random numbers to be the same on every run. In that case, you seed the generator with a constant value by calling an overload of SEED. To produce different output for every run, simply omit the seed call. Then the system chooses a suitable seed for you.
DBMS_RANDOM Subprograms
Table 6-1 summarizes the DBMS_RANDOM subprograms, followed by a full description of each subprogram.
Table 6-1 DBMS_RANDOM Package Subprograms
| Subprogram | Description |
|---|---|
|
Initializes the package with a seed value. |
|
|
Returns random numbers in a normal distribution. |
|
|
Generates a random number. |
|
|
Resets the seed. |
|
|
Gets a random string. |
|
|
Terminates package. |
|
|
One version gets a random number greater than or equal to 0 and less than 1, with 38 digits to the right of the decimal point (38-digit precision). The other version gets a random Oracle Database number |
Note:
-
The INITIALIZE Procedure, RANDOM Function and TERMINATE Procedure are deprecated. They are included in this release for legacy reasons only.
-
The
PLS_INTEGERandBINARY_INTEGERdata types are identical. This document usesBINARY_INTEGERto indicate data types in reference information (such as for table types, record types, subprogram parameters, or subprogram return values), but may use either in discussion and examples. -
The
INTEGERandNUMBER(38)data types are also identical. This document usesINTEGERthroughout.
INITIALIZE Procedure
This procedure is deprecated. Although currently supported, it should not be used. It initializes the random number generator.
Syntax
DBMS_RANDOM.INITIALIZE (
val IN BINARY_INTEGER);Parameters
Table 6-2 INITIALIZE Procedure Parameters
| Parameter | Description |
|---|---|
|
|
Seed number used to generate a random number |
Usage Notes
This procedure is obsolete as it simply calls the SEED Procedure.
NORMAL Function
This function returns random numbers in a standard normal distribution.
Syntax
DBMS_RANDOM.NORMAL
RETURN NUMBER;Return Value
The random number, a NUMBER value
RANDOM Function
This procedure is deprecated. Although currently supported, it should not be used. It generates and returns a random number.
Syntax
DBMS_RANDOM.RANDOM
RETURN binary_integer;Return Value
A random BINARY_INTEGER value greater than or equal to -power(2,31) and less than power(2,31)
Usage Notes
See the NORMAL Function and the VALUE Function.
SEED Procedure
This procedure resets the seed used in generating a random number.
Syntax
DBMS_RANDOM.SEED (
val IN BINARY_INTEGER);
DBMS_RANDOM.SEED (
val IN VARCHAR2);Parameters
Table 6-3 SEED Procedure Parameters
| Parameter | Description |
|---|---|
|
|
Seed number or string used to generate a random number |
Usage Notes
The seed can be a string up to length 2000.
STRING Function
This function generates and returns a random string.
Syntax
DBMS_RANDOM.STRING
opt IN CHAR,
len IN NUMBER)
RETURN VARCHAR2;Parameters
Table 6-4 STRING Function Parameters
| Parameter | Description |
|---|---|
|
|
What the returning string looks like:
Otherwise the returning string is in uppercase alpha characters. |
|
|
Length of the returned string |
Return Value
A VARCHAR2 value with the random string
TERMINATE Procedure
This procedure is deprecated. Although currently supported, it should not be used. It would be called when the user is finished with the package.
Syntax
DBMS_RANDOM.TERMINATE;VALUE Function
NUMBER value x, where x is greater than or equal to the specified low value and less than the specified high value.
Syntax
DBMS_RANDOM.VALUE
RETURN NUMBER;
DBMS_RANDOM.VALUE(
low IN NUMBER,
high IN NUMBER)
RETURN NUMBER;Parameters
Table 6-5 VALUE Function Parameters
| Parameter | Description |
|---|---|
|
|
Lower limit of the range in which to generate a random number |
|
|
Upper limit of the range in which to generate a random number |
Return Value
A NUMBER value that is the generated random number