開發過程中經常會有產生隨機數的需求,而oracle本身自帶的有這樣的功能,下面介紹一下擁有此功能的包DBMS_RANDOM,主要以官方文檔來說明:
The DBMS_RANDOM
package
provides a built-in random number generator.
該包裏包含了幾個子程序,這裏僅僅介紹兩個:
RANDOM function
This procedure is deprecated. Although currently supported, it should not be used. It generates and returns a random number.
DBMS_RANDOM.RANDOM RETURN binary_integer;
A random BINARY_INTEGER
value greater than or equal to -power(2,31)
and less than power(2,31)
VALUE function
One version returns a random number, greater than or equal to 0 and less than 1, with 38 digits to the right of the decimal (38-digit precision). The other version returns a random Oracle Database NUMBER
value x
,
where x
is greater than or equal to the specified low
value and less than the specified high
value.
DBMS_RANDOM.VALUE RETURN NUMBER; DBMS_RANDOM.VALUE( low IN NUMBER, high IN NUMBER) RETURN NUMBER;
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 |
A NUMBER
value that is the generated random number
舉例:round(dbms_random.value(0,18),0) 就可以返回大於等於0且小於18的整數;