The DBMS_RANDOM package in Oracle encapsulates some functions that generate random numbers and random strings. The following are commonly used:
DBMS_RANDOM.VALUE Function
This function is used to generate a random number. It can be used in either of the following ways:
1. Generate a random number with 38-bit precision between 0 and 1 (excluding 0 and 1). Syntax:
| DBMS_RANDOM.VALUE return number; |
This usage does not contain parameters.
2. Generate a random number with 38-bit precision within the specified range. Syntax:
| DBMS_RANDOM.VALUE (low in number, high in number) return number; |
This usage includes two parameters. The low parameter is used to specify the lower limit of the random number to be generated, and the high parameter specifies the upper limit to generate the random number. Note that the random number generated may be equal to the lower limit, but it is definitely smaller than the upper limit, that is, "low <= Random Number
For example, to generate a number between 1 and 100, write DBMS_RANDOM.VALUE (1,100)
DBMS_RANDOM.STRING Function
This function generates a random string with the Syntax:
| DBMS_RANDOM.STRING (opt in char, len in number) RETURN VARCHAR2; |
The len parameter specifies the length of the generated string.
The opt parameter specifies the style of the generated string. The following table lists the allowed values and their meanings:
| Value |
Description |
| 'U' or 'U' |
Returns a string consisting of uppercase letters. |
| 'L' or 'l' |
Returns a string consisting of lowercase letters. |
| 'A' or 'A' |
Returns a string consisting of uppercase and lowercase letters. |
| 'X' or 'X' |
Returns a string consisting of uppercase letters and numbers. |
| 'P' or 'P' |
Returns a string consisting of any printable characters. |
For example, to generate a string with a length of 6 and only uppercase letters, you can write it as follows: DBMS_RANDOM.STRING ('U', 6)