Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of Oracle Random function

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

To put it simply, most of the random numbers taken by Oracle are generated by calling dbms_random package. There are four ways to generate random numbers through dbms_random package:

1 、 dbms_random.normal

This function takes no arguments and is used to generate a 38-bit precision random number between-1 and 1, which returns a number type of normal distribution, so basically the random number will be between-1 and 1.

After a simple test, it produces 100000 times with a maximum capacity of about 5:

SQL > select dbms_random.normal from dual; NORMAL-0.6940304577580136806344822459508580046783

2 、 dbms_random.random

This also has no parameters and returns an integer value from-power (2pj31) to power (2pj31) (--2 to the 31st power)

3 、 dbms_random.value

This function is divided into two types. One is a random number with a precision of 38 bits between 0 and 1 if it has no arguments.

SQL > select dbms_random.value from dual; VALUE-- 0.58983014999643548701631750396301271752

The second is to add two parameters a, b, and return a random number with 38-bit precision between an and b.

SQL > select dbms_random.value (100500) value from dual; VALUE-- 412.150194612502916808701157054098274240

Note: no matter the first few places, there are 38 places after the decimal point.

In addition, if you want to get an integer: ROUND (dbms_random.value (1500), 0)

4 、 dbms_random.string

This function produces a random string that must take two parameters and the syntax is: dbms_random.string (opt in CHAR,len in NUMBER)

The parameter len specifies the length of the generated string (up to 60)

The parameter opt specifies the style of the generated string, and the allowed values and their representations are shown in the following table:

Take a value

Meaning

'u'or'U'

Returns a string of uppercase letters

'l' or'L'

Returns a string of lowercase letters

'a'or'A'

Returns a string of uppercase and lowercase letters

'x'or'X'

Returns a string of uppercase letters and numbers

'p'or'P'

Returns a string of arbitrary printable characters

For example, to produce a string that is 6 in length and contains only uppercase letters, you can write: DBMS_RANDOM.STRING ('Upright pencils 6)

SQL > SELECT DBMS_RANDOM.string ('Upright Magazine 6) FROM DUAL; DBMS_RANDOM.STRING (' Upright Magazine 6)-- ECQALV

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report