All Products
Search
Document Center

PolarDB:DBMS_RANDOM

Last Updated:Mar 27, 2024

The built-in DBMS_RANDOM package provides the built-in random number generator.

Subprograms

Subprogram

Description

INITIALIZE Procedure

Initializes the DBMS_RANDOM package by using a specified seed value.

NORMAL Function

Returns a random number that follows a normal distribution.

RANDOM Function

Returns a random number of the INTEGER type.

SEED Procedures

Resets the seed value for the DBMS_RANDOM package.

STRING Function

Returns a random string of the VARCHAR2 type in a specified format.

TERMINATE Procedure

Terminates the use of the package.

VALUE Functions

Returns a random number of the NUMBER type within a specified range.

INITIALIZE Procedure

This stored procedure is used to initialize the DBMS_RANDOM package by using a specified seed value.

Syntax

DBMS_RANDOM.INITIALIZE(val IN INTEGER);

Parameters

Parameter

Description

val

Generates the specified seed value for the random number.

Examples

The following example shows how to initialize the seed value of the DBMS_RANDOM package by using a specified number:

EXEC DBMS_RANDOM.INITIALIZE(100);

NORMAL Function

This function is used to return a random number of the NUMBER type that follows a normal distribution.

Syntax

DBMS_RANDOM.NORMAL() RETURN NUMBER;

Return values

Return value

Description

NUMBER

The random number that follows a normal distribution.

Examples

The following example shows how to retrieve a random number that follows a normal distribution:

SELECT DBMS_RANDOM.NORMAL() FROM DUAL;
        normal        
----------------------
 -0.48004454788674555
(1 row)

RANDOM Function

This function is used to return a random number of the INTEGER type.

Syntax

DBMS_RANDOM.RANDOM RETURN INTEGER;

Return values

Return value

Description

INTEGER

The returned random integer.

Examples

The following example shows how to retrieve a random integer:

SELECT DBMS_RANDOM.RANDOM() FROM DUAL;
   random   
------------
 1078289776
(1 row)

SEED Procedures

This stored procedure is used to reset the seed value.

Syntax

DBMS_RANDOM.SEED(val IN INTEGER);
DBMS_RANDOM.SEED(val IN VARCHAR2);

Parameters

Parameter

Description

val

The input value of the INTEGER or VARCHAR type that is used to specify a new seed value.

Examples

The following example shows how to use this stored procedure to reset the seed value:

EXEC DBMS_RANDOM.SEED(1);
EXEC DBMS_RANDOM.SEED('test seed');

STRING Function

This function is used to return a random string of the VARCHAR2 type in a specified format.

Syntax

DBMS_RANDOM.STRING(
   opt  IN  CHAR,
   len  IN  NUMBER)
  RETURN VARCHAR2;

Parameters

Parameter

Description

opt

The option that is used to format the return value. Valid values:

  • u or U: a string of uppercase letters.

  • l or L: a string of lowercase letters.

  • a or A: a string of uppercase and lowercase letters.

  • x or X: a string of uppercase letters and digits.

  • P or P: a string of printable characters.

len

The length of the return value.

Return values

Return value

Description

VARCHAR2

The returned random string.

Examples

The following example shows how to retrieve a random string in a specified format:

SELECT DBMS_RANDOM.STRING('X', 10) FROM DUAL;
   string   
------------
 MD6LDO7JLJ
(1 row)

TERMINATE Procedure

This stored procedure is used to terminate the use of the DBMS_RANDOM package.

Syntax

DBMS_RANDOM.TERMINATE();

Examples

EXEC DBMS_RANDOM.TERMINATE;

VALUE Functions

This function is used to return a random number of the NUMBER type within a specified range.

Syntax

DBMS_RANDOM.VALUE() RETURN NUMBER;

DBMS_RANDOM.VALUE(
  low  IN  NUMBER,
  high IN  NUMBER)
RETURN NUMBER;

Parameters

Parameter

Description

low

(Optional) The lower boundary of the generated random number. The generated random number is greater than or equal to the lower boundary.

high

(Optional) The upper boundary of the generated random number. The generated random number is less than the upper boundary.

Return values

Return value

Description

NUMBER

The returned random number.

Examples

The following example shows how to retrieve a random number within a specified range:

SELECT DBMS_RANDOM.VALUES(0.0, 10) FROM DUAL;
       value       
-------------------
 8.401877167634666
(1 row)