Oracle Random number _oracle

Source: Internet
Author: User
Using Oracle's Dbms_random package combined with rownum to achieve, for example, under random access to 499 households:
SELECT * FROM
(SELECT * from Busi.t_ar_userinfo ORDER by Dbms_random.value)
where RowNum < 500;

References to Dbms_random, links to: http://www.psoug.org/reference/dbms_random.html

Deprecated. Using the methods in the Dbms_crypto built-in package, this package has not been recommended for use

Attached, dbms_random several parameters of the introduction:
function value return number, which returns the random numbers between [0,1], with a precision of 38 digits (Gets a random number, greater than or equal to 0 and less than 1, with Decimal digits)
function value (low in Numvber,high in number) returns, returning a random count between [Low,high]
function normal return Number,return random numbers in a standard normal distribution, returns a group of the normal distribution, the standard deviation is 1, the expected value is 0, and the return values are 68% Between +1 and-1, 95% is between +2 and-2, and 99% is between +3 and 3.
function random return Binary_integer, (Generate random Numeric Values),
function string (opt char,length number) return VARCHAR2 (the maximum is 60) returns a string of the specified length (Create Random Strings), opt seed V Alues:
' A ', ' a ' &n

Q: I work the problem: The supervisor let me for an event to randomly remove some qualified email or mobile phone number users to issue award-winning notice or other information, our company's Oracle 9i How to achieve this?
A: You can use ORACLE to generate random numbers of Pl/sql, directory file name in:/oracle_home/rdbms/admin/dbmsrand.sql.
To compile under the SYS user before using:
Sql>@/oracle_home/rdbms/admin/dbmsrand.sql
It actually generates a DBMS_RANDOM package under the SYS user, generates a public synonym, and authorizes all database users to have permission to execute.
Using the Dbms_random package, the method of taking random data out:
1. First create a unique growth serial number tmp_id
Create sequence tmp_id increment by 1-start with 1 maxvalue 9999999 nocycle;
2. Then create a temporary table tmp_1 to take out all the records that meet the conditions of this activity.
CREATE TABLE Tmp_1 as select Tmp_id.nextval as Id,email,mobileno from table name where condition;
Find the largest ID number:
Select Max (id) from tmp_1;
assumed to be 5000
3. Set a seed to generate random numbers
Execute Dbms_random.seed (12345678);
Or
Execute Dbms_random.seed (to_char (sysdate, ' mm-dd-yyyy HH24:MI:SS '));
4. Call random number generation function Dbms_random.value generate temporary table tmp_2
Suppose you randomly take 200
CREATE TABLE tmp_2 as Select Trunc (Dbms_random.value (1,5000)) as ID from tmp_1 where rownum<201;
[Description: Dbms_random.value (1,5000) is a random number between 1 and 5000, with decimals,
The trunc function takes a random number rounding to correspond to the integer ID field of a temporary table.
Note: If the tmp_1 record is more (100,000 or more), you can also find a table of about 200 rows (if it is tmp_3) to generate tmp_2
CREATE TABLE tmp_2 as Select Trunc (Dbms_random.value (1,5000)) as ID from Tmp_3 where rownum<201; ]
5. Tmp_1 and tmp_2 associated with the acquisition of eligible 200 users
Select T1.mobileno,t1.email from Tmp_1 t1,tmp_2 T2 where t1.id=t2.id;
[Note: If the tmp_1 record is more than 100,000, you need to index the ID field.] ]
You can also output to a text file:
Set pagesize 300;
Spool/tmp/200.txt;
Select T1.mobileno,t1.email from Tmp_1 t1,tmp_2 T2 where t1.id=t2.id order by T1.mobileno;
Spool off;
6. After use, delete temporary table tmp_1, tmp_2 and serial number tmp_id.
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.