| Oracle DBMS_RANDOM Version 26ai |
|---|
| General Information | |||||||||||||||||||
| Library Note |
|
||||||||||||||||||
| Purpose | Generation of random strings and numbers | ||||||||||||||||||
| Note | When possible it is preferable to use the functionality in DBMS_CRYPTO | ||||||||||||||||||
| AUTHID | DEFINER | ||||||||||||||||||
| Dependencies |
|
||||||||||||||||||
| Documented | Yes: Packages and Types Reference | ||||||||||||||||||
| First Available | 8.0 | ||||||||||||||||||
| Security Model | Owned by SYS with EXECUTE granted to PUBLIC | ||||||||||||||||||
| Source | {ORACLE_HOME}/rdbms/admin/dbmsrand.sql | ||||||||||||||||||
| Subprograms | |||||||||||||||||||
| INITIALIZE | |||||||||||||||||||
| Initialize package with a seed value | dbms_random.initialize(seed IN BINARY_INTEGER); |
||||||||||||||||||
exec dbms_random.initialize(17809465); |
|||||||||||||||||||
| NORMAL | |||||||||||||||||||
| Returns random numbers in a standard normal distribution | dbms_random.normal RETURN NUMBER PARALLEL_ENABLE; |
||||||||||||||||||
SELECT dbms_random.normal; |
|||||||||||||||||||
| RANDOM | |||||||||||||||||||
| Generate Random Numeric Values | dbms_random.random RETURN BINARY_INTEGER PARALLEL_ENABLE; |
||||||||||||||||||
conn sys@pdbdev as sysdba |
|||||||||||||||||||
| Force Output To Positive Values | SELECT (1+ABS(MOD(dbms_random.random,100000))); |
||||||||||||||||||
| RECORD_RANDOM_NUMBER | |||||||||||||||||||
| Officially undocumented: External C function to record random value | dbms_random.record_random_number(val IN NUMBER); |
||||||||||||||||||
DECLARE |
|||||||||||||||||||
| REPLAY_RANDOM_NUMBER | |||||||||||||||||||
| Officially undocumented: External C function to replay random value | dbms_random.replay_random_number(RETURN NUMBER; |
||||||||||||||||||
See RECORD_RANDOM_NUMBER Demo Above |
|||||||||||||||||||
| SEED | |||||||||||||||||||
| Reset the seed value Overload 1 |
dbms_random.seed(val IN BINARY_INTEGER); |
||||||||||||||||||
exec dbms_random.seed(681457802); |
|||||||||||||||||||
| Overload 2 | dbms_random.seed(val IN VARCHAR2); |
||||||||||||||||||
exec dbms_random.seed('o42i4p'); |
|||||||||||||||||||
| STRING | |||||||||||||||||||
| Create Random Strings | dbms_random.string(opt IN CHAR, len IN NUMBER) RETURN VARCHAR2 PARALLEL_ENABLE; |
||||||||||||||||||
CREATE TABLE random_strings AS |
|||||||||||||||||||
-- create test data |
|||||||||||||||||||
| TERMINATE | |||||||||||||||||||
| Reset the package ... essentially each call makes it serially reusable by resetting internal variables | dbms_random.terminate; |
||||||||||||||||||
exec dbms_random.terminate; |
|||||||||||||||||||
| VALUE | |||||||||||||||||||
| Gets a random number, greater than or equal to 0 and less than 1, with decimal 38 digits Overload 1 |
dbms_random.value RETURN NUMBER PARALLEL_ENABLE; |
||||||||||||||||||
col value format 9999999999 |
|||||||||||||||||||
| Alternatively, you can get a random Oracle number x, where x is greater than or equal to low and less than high Overload 2 |
dbms_random.value(low NUMBER, high NUMBER) RETURN NUMBER PARALLEL_ENABLE; |
||||||||||||||||||
SELECT dbms_random.value(2, 3); |
|||||||||||||||||||
| Select a random record | SELECT srvr_id |
||||||||||||||||||
| Demo | |||||||||||||||||||
| The PL/SQL at right, in a LOOP, will generate random numbers without using DBMS_RANDOM or DBMS_CRYPTO based on a seed value selected from a table or provided through an input parameter | IF seed=0 THEN |
||||||||||||||||||