DBMS_RANDOM¶
The DBMS_RANDOM package provides a number of methods to generate random
values. The procedures and functions available in the DBMS_RANDOM
package are listed in the following table.
Function/Procedure |
Return Type |
Description |
|
n/a |
Initializes the |
|
|
Returns a random |
|
|
Returns a random |
|
n/a |
Resets the seed with the specified |
|
n/a |
Resets the seed with the specified |
|
|
Returns a random string. |
|
n/a |
|
|
|
Returns a random number with a value greater than or equal to |
|
|
Returns a random number with a value greater than or equal to |
INITIALIZE¶
The INITIALIZE procedure initializes the DBMS_RANDOM package with a
seed value. The signature is:
INITIALIZE(<val> IN INTEGER)
This procedure should be considered deprecated; it is included for backward compatibility only.
Parameters
val
valis the seed value used by theDBMS_RANDOMpackage algorithm.
Example
The following code snippet demonstrates a call to the INITIALIZE
procedure that initializes the DBMS_RANDOM package with the seed value,
6475.
DBMS_RANDOM.INITIALIZE(6475);
NORMAL¶
The NORMAL function returns a random number of type NUMBER. The
signature is:
<result> NUMBER NORMAL()
Parameters
result
resultis a random value of typeNUMBER.
Example
The following code snippet demonstrates a call to the NORMAL function:
x:= DBMS_RANDOM.NORMAL();
RANDOM¶
The RANDOM function returns a random INTEGER value that is greater than
or equal to -2 ^31 and less than 2 ^31. The signature is:
<result> INTEGER RANDOM()
This function should be considered deprecated; it is included for backward compatibility only.
Parameters
result
resultis a random value of type INTEGER.
Example
The following code snippet demonstrates a call to the RANDOM function.
The call returns a random number:
x := DBMS_RANDOM.RANDOM();
SEED¶
The first form of the SEED procedure resets the seed value for the
DBMS_RANDOM package with an INTEGER value. The SEED procedure is
available in two forms; the signature of the first form is:
SEED(<val> IN INTEGER)
Parameters
val
valis the seed value used by theDBMS_RANDOMpackage algorithm.
Example
The following code snippet demonstrates a call to the SEED procedure;
the call sets the seed value at 8495.
DBMS_RANDOM.SEED(8495);
SEED¶
The second form of the SEED procedure resets the seed value for the
DBMS_RANDOM package with a string value. The SEED procedure is
available in two forms; the signature of the second form is:
SEED(<val> IN VARCHAR2)
Parameters
val
valis the seed value used by theDBMS_RANDOMpackage algorithm.
Example
The following code snippet demonstrates a call to the SEED procedure;
the call sets the seed value to abc123.
DBMS_RANDOM.SEED('abc123');
STRING¶
The STRING function returns a random VARCHAR2 string in a user-specified
format. The signature of the STRING function is:
<result> VARCHAR2 STRING(<opt> IN CHAR, <len> IN NUMBER)
Parameters
opt
Formatting option for the returned string.
optionmay be:
Option |
Specifies Formatting Option |
|---|---|
|
Uppercase alpha string |
|
Lowercase alpha string |
|
Mixed case string |
|
Uppercase alpha-numeric string |
|
Any printable characters |
len
The length of the returned string.
result
resultis a random value of typeVARCHAR2.
Example
The following code snippet demonstrates a call to the STRING function;
the call returns a random alpha-numeric character string that is 10
characters long.
x := DBMS_RANDOM.STRING('X', 10);
TERMINATE¶
The TERMINATE procedure has no effect. The signature is:
TERMINATE
The TERMINATE procedure should be considered deprecated; the procedure
is supported for compatibility only.
VALUE¶
The VALUE function returns a random NUMBER that is greater than or equal
to 0, and less than 1, with 38 digit precision. The VALUE function has
two forms; the signature of the first form is:
<result> NUMBER VALUE()
Parameters
result
resultis a random value of typeNUMBER.
Example
The following code snippet demonstrates a call to the VALUE function.
The call returns a random NUMBER:
x := DBMS_RANDOM.VALUE();
VALUE¶
The VALUE function returns a random NUMBER with a value that is between
user-specified boundaries. The VALUE function has two forms; the
signature of the second form is:
<result> NUMBER VALUE(<low> IN NUMBER, <high> IN NUMBER)
Parameters
low
lowspecifies the lower boundary for the random value. The random value may be equal tolow.
high
highspecifies the upper boundary for the random value; the random value will be less thanhigh.
result
resultis a random value of typeNUMBER.
Example
The following code snippet demonstrates a call to the VALUE function.
The call returns a random NUMBER with a value that is greater than or
equal to 1 and less than 100:
x := DBMS_RANDOM.VALUE(1, 100);