Oracle Database random number generation function

Source: Internet
Author: User

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)

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.