Oracle random number Dbms_random packet _oracle

Source: Internet
Author: User
Tags numeric

Simply put, there are roughly 4 ways to call random numbers via Dbms_random packets:

1, Dbms_random.normal

This function, without parameters, returns a number type of normal distribution, so the random numbers are basically between 1 and 1.
A simple test, resulting in 100,000 times the maximum can be about 5:

SQL code

 declare 
  i number:=;  
  J number:=;  
 Begin with 
  K in.. loop  
  i:= dbms_random.normal;  
   If i > J  
    then j:=i;  
   End If;  
  End Loop;  
  Dbms_output.put_line (j);  
 

5.15325081797418404136433867107468983182

Pl/sql procedure successfully completed

2, Dbms_random.random

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

3, Dbms_random.value

This function is divided into two types, one with no arguments, and returns the 38-bit decimal between >=0 and <1 directly.

  SQL > Column value format 9.99999999999999999999999999999999999999
  SQL > select Dbms_random.value from dual;< C2/>value
  -----------------------------------------
   . 58983014999643548701631750396301271752

The second is the addition of two parameters A, B, the return value of 38 decimal places between >=a and <b

  SQL > Column value format 999.999999999999999999999999999999999999
  SQL > select Dbms_random.value (100,500) value from dual;
                    VALUE
  -----------------------------------------
   412.150194612502916808701157054098274240

Note: No matter the first few, after the decimal point is 38 digits

You can use the trunc () function to take the whole

4, Dbms_random.string

This function must have two arguments, the preceding character specifies the type, and the following number specifies the number of digits (maximum 60)

Type description:

 "> ' u ', ' u ': upper case alpha characters only ' l ', ' l ': lower case alpha characters  ' A ', ' a ': alpha characters only [mixed case] ' x ', ' x ': any alpha-numeric characters (upper) ' P ', ' P ': any printable
  Characters SQL > column value format A30 SQL > select dbms_random.string (' U ') value from dual; VALUE------------------------------Vtqnlgiselpxedbxkuzlxkbajmutia SQL > select dbms_random.string (' l ',) valu
  e from dual; VALUE------------------------------uqygsbquingfqdytpgjvdoblxeglgu SQL > select dbms_random.string (' a ',) valu
  e from dual; VALUE------------------------------ngtgkqypuswhbfcrhiolqwouxkqjjy SQL > select dbms_random.string (' x ',) valu
  e from dual; VALUE------------------------------uvwonyjmxt31vefpd736wjcj5qt6bd SQL > select dbms_random.string (' P ',) Valu
  e from dual; VALUE------------------------------: mak$ (wt4m_7c/+f[_xuscf$p zcq{

5. About Seed

Seed can be set to determine the starting point for random numbers, and any change in random numbers will be determined for the same seed.

That is, if you call seed at some point, the first random number is 4, the second is 6, and the third is 1, so when you call the same seed again, the random number that you generate at once is 4, 6, 1.

There are two kinds of seed, one is numeric, the other is character type (maximum length 2000)

  --seed with a binary integer
  PROCEDURE seed (Val in binary_integer);
  PRAGMA restrict_references (seed, wnds);

  --seed with a string (up to length)
  PROCEDURE seed (Val in VARCHAR2);
  PRAGMA restrict_references (seed, wnds);

6. About Initialize

An integer argument, the note says it's clear:

 --Obsolete, just calls seed (val)
  PROCEDURE Initialize (val in binary_integer);
  PRAGMA restrict_references (Initialize, wnds);

This article introduces you to the Oracle random number Dbms_random package, this is the end, I hope to learn more Oracle random number help.

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.