The DBMS_RANDOM package provides users with random numbers. When DBMS_RANDOM is used, the RANDOM number seed is provided. The so-called RANDOM number seed is the value used in the RANDOM algorithm. In many programming languages, the seeds of two random numbers are the same, so the random number sequence is also the same. Therefore, it is necessary to provide different seeds to obtain the sequence when DBMS_RANDOM is used to obtain the random number. The procedures and functions in the DMBS_RANDOM package are as follows:
1) INITIALIZE:
This process initializes the DMBS_RANDOM package and must initialize the random seed. The syntax is as follows:
DBMS_RANDOM.INITIALIZE (seed in BINARY_INTERGER)
2) SEED
This process is used to reset the random seed. The syntax is as follows:
DBMS_RANDOM.SEED (seed in BINARY_INTEGER );
3) RANDOM
This function is used to generate a random number. The syntax is as follows:
DBMS_RANDOM.RANDOM RETURN BINARY_INTEGER;
4) TERMINATE
This process is used to close the DBMS_RANDOM package. The syntax is as follows:
DBMS_RANDOM.TERMINATE;
The following is an example of how to obtain a random number:
Set serverout on;
DECLARE
N_randomNum NUMBER (10 );
N_randomSeed constant number: = 98765;
BEGIN
Dbms_random.initialize (n_randomSeed );
FOR I IN 0 .. 10 LOOP
N_randomNum: = abs (dbms_random.random ()/n_randomSeed );
Dbms_output.put_line (n_randomNum );
End loop;
Dbms_random.terminate;
END;
Returns an integer between 0 and 10:
Select ceil (DBMS_RANDOM.VALUE (0, 10) from dual;
Get a decimal point between 0 and 1:
SELECT dbms_random.VALUE FROM dual;
Returns a decimal number between 0 and 100:
SELECT dbms_random.VALUE (0,100) FROM dual;
Obtain a random number composed of characters and numbers (the first parameter can be set to 'U', 'U': uppercase letter, 'l', 'L ': lowercase letters, 'A', 'A': uppercase letters, lowercase letters, 'x', 'x': Numbers, uppercase letters, 'P', 'P ': printable characters ,):
Select dbms_random.string ('x', 3) from dual;