Solved

How to create random number using oracle ?

Posted on 2007-11-22
6
5,038 Views
Last Modified: 2013-12-18
I want to create a random number between 1 to n and return any number within the range using sql. Our database is oracle 10g.
0
Comment
Question by:KG1973
6 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 20333280
0
 
LVL 27

Accepted Solution

by:
sujith80 earned 100 total points
ID: 20333283
Use the dbms_random package.

E.g.
SQL> select round(dbms_random.value(1, 100)) from dual;

ROUND(DBMS_RANDOM.VALUE(1,100))
-------------------------------
                             68

SQL> select round(dbms_random.value(1, 100)) from dual;

ROUND(DBMS_RANDOM.VALUE(1,100))
-------------------------------
                             31
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 20333290
with the below code, i am able to generate 10 random numbers between 1 and 10

SET SERVEROUTPUT ON SIZE 1000000
DECLARE
  l_seed  BINARY_INTEGER;
BEGIN
  l_seed := TO_NUMBER(TO_CHAR(SYSDATE,'YYYYDDMMSS'));
  DBMS_RANDOM.initialize (val => l_seed);
  FOR cur_rec IN 1 ..10 LOOP
    DBMS_OUTPUT.put_line('value(low => 1, high => 10): ' || round(to_number(TO_CHAR(DBMS_RANDOM.value(low => 1, high => 10)))));
  END LOOP;
  DBMS_RANDOM.terminate;
END;
/

Thanks
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

Assisted Solution

by:awking00
awking00 earned 25 total points
ID: 20340423
You can create a function -
create or replace function rand(n_in in number)
return number is
ret_n  number;
begin
ret_n := round(dbms_random.value(1,n_in));
return ret_n;
end;
/
Then input value for n
For example,
SQL> select rand(80) from dual;
  RAND(80)
----------
        60
SQL> /
  RAND(80)
----------
        36
,etc.
0
 
LVL 1

Author Closing Comment

by:KG1973
ID: 31410527
Perfect solution.
0
 
LVL 1

Author Comment

by:KG1973
ID: 20347505
You just solved my problem. Thank you very much.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to recover a database from a user managed backup

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now