Oracle random function

tags: Oracle  SQL  J#

These days, when doing barcodes, you need to generate large amounts of data in the database, involving random strings, such as: mixed numbers of letters and numbers, which involves random packages of oracle. The application, the following reprint an article, and make the necessary changes!
Basic understanding

The files for these functions and the DBMS_RANDOM package are included in SQLPlus:


 select text   from all_source 
where name = 'DBMS_RANDOM'
and type = 'PACKAGE' order by line;



◆ TYPE num_array
◆ PROCEDURE terminate
◆ PROCEDURE seed
◆ PROCEDURE initialize
◆ FUNCTION random
◆ FUNCTION value RETURN NUMBER;
◆ FUNCTION value (low IN NUMBER, high IN NUMBER) RETURN NUMBER;
◆ FUNCTION normal RETURN NUMBER;
◆ FUNCTION string (opt char, len NUMBER) RETURN VARCHAR2;


2. Application examples

SELECT DBMS_RANDOM.RANDOM FROM DUAL;


Further requirements, for example, to generate a random number of 0-100, a little workaround:

select abs(mod(dbms_random.random,100)) from dual 


3. Advanced instructions

Dbms_random has new functions to achieve these functions
FUNCTION value RETURN NUMBER;
FUNCTION value (low IN NUMBER, high IN NUMBER) RETURN NUMBER;
FUNCTION normal RETURN NUMBER;
FUNCTION string (opt char, len NUMBER) RETURN VARCHAR2;

Generate a random number between N and M

SELECT   DBMS_RANDOM.VALUE(N,M) FROM DUAL; 

The default DBMS_RANDOM.VALUE returns a random number between 0 and 1.
select dbms_random.value, dbms_random.value(55,100) from dual;


VALUE DBMS_RANDOM.VALUE(55,100)
--------------- -----------------------------
0.714469037747011 68.5593418279622

The NORMAL function returns a set of numbers that follow a normal distribution. This normal distribution has a standard deviation of 1, and an expected value of zero. 68% of the values ​​returned by this function are between -1 and +1, 95% between -2 and +2, and 99% between -3 and +3.
Finally, it is the STRING function. It returns a random string of up to 60 characters.

Generate text and date values ​​with DBMS_RANDOM

Numbers, text strings, and dates are three common types of data that users will encounter in a table. Although you can generate numbers randomly using DBMS_RANDOM in the PL/SQL package - it does that - it also generates text and date values ​​randomly.

Generate random numbers
Let us start with numbers. The VALUE function returns a number greater than or equal to 0 but less than 1, with a precision of 38 bits.

SELECT DBMS_RANDOM.VALUE FROM DUAL;


For integers within the specified range, the parameters low_value and high_value are added and the decimal is truncated from the result (the maximum cannot be used as a possible value). So for integers between 0 and 99, you would use the following code:
SELECT TRUNC(DBMS_RANDOM.VALUE(0, 100)) FROM DUAL; 


2. Generate random text strings
To randomly generate a text string, use the STRING function and write code to specify the type of string and the desired length:

SELECT DBMS_RANDOM.STRING('A', 20) FROM DUAL; 



The type code is described in the Oracle Database 10g PL/SQL Packages and Types Reference.

Here are some types of code:

-- 'u', 'U' - returns all uppercase strings
-- 'l', 'L' - returns all lowercase strings
-- 'a', 'A' - return a case-insensitive string
-- 'x', 'X' - returns a string of all uppercase and numeric characters
-- 'p', 'P' - returns a random combination of characters appearing on the keyboard


Comprehensive example:

SELECT   TRUNC (DBMS_RANDOM.VALUE (1, 10)), 
DBMS_RANDOM.string ('~', 10),
DBMS_RANDOM.string ('L', 10),
DBMS_RANDOM.string ('U', 10),
DBMS_RANDOM.string ('A', 10),
DBMS_RANDOM.string ('X', 10),
DBMS_RANDOM.string ('P', 10)
FROM ( SELECT LEVEL, ROWNUM rn
FROM DUAL
CONNECT BY ROWNUM <= 100);


Running the above sql everything is very clear
3. Generate random dates
Oracle saves the date as an integer offset from a key date in the past (if you are curious, I can tell you that this date is January 1, 4712 BC). This means that you can randomly generate a date within a specified range by looking for an integer corresponding to the start date you want and then adding a random integer to it.

Using the TO_CHAR function and the ‘J’ format code, you can generate an internal date number for today's date:

SELECT TO_CHAR(SYSDATE, 'J') FROM DUAL; 


For example, to generate an arbitrary date in 2003, you can first determine the date integer on January 1, 2003;

SELECT TO_CHAR(TO_DATE('01/01/03','mm/dd/yy'),'J')FROM DUAL; 



The result given by the system is 2546641. So to generate any date for the year, we will use DBMS_RANDOM.VALUE with low_value equals 2546641 and high_value equals 2546641 + 364 parameters, and then convert it to date:

SELECT TO_DATE(TRUNC(DBMS_RANDOM.VALUE(2452641,2452641+364)),'J') FROM DUAL;

Intelligent Recommendation

Random random function use

Use of random random functions Ability to master Ability to use random seeds to generate "determined" random numbers Able to generate random integers Ability to sequence list operations A ra...

random function random numpy

random function random numpy numpy.random.rand() numpy.random.randn() numpy.random.randint() numpy.random.random_integers() numpy.random.choice() numpy.random.rand() rand () to generate a given dimens...

The random function random python

at the top of this blog which, the author explains the role of several functions 1. random.random(): Random floating point for generating a 0 to 1: 0 <= n <1.0 2. random.uniform(a,b): for genera...

Py3 --- random function (Random)

table of Contents   1、random.choice(sql) Definition: a random element taken from the sequence elements 2、random.random() Definition: Returns a random real number generated in [0, 1) in the range ...

More Recommendation

python random function random

Python generates random values-random module … … import random (a module that generates random values) Shuffle function: An ordered list a = [1,2,3,4,5,6] a = random.shuffle(a) print (a)...

random pseudo-random function

There are six rows in the class, denoted by A, B, C, D, E, F, and there are five people in each row, denoted by 1, 2, 3, 4, and 5 respectively. In order to draw people to answer questions fairly, a ha...

Random function (random number)

Math.random () is a random function: any number of randomly takes 0 to 1; 1, four rounds into functions If it is an integer, then you will use a four-way function: math.Round (); For example: 2, rando...

Delphi random () random function

Delphi random () random function Function prototype: Function Random (Range: Integer): Integer; Parameters: RANGE: Integer, Return value: integer, its range: 0 <= random (RANGE) <RANGE (Specify ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top