Use DBMS_RANDOM to obtain random

Source: Internet
Author: User
Tags printable characters random seed

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.