Solved

How to create random number using oracle ?

Posted on 2007-11-22
6
5,055 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 32

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
database upgrade 8 112
PL SQL Developer 7 54
Create file system directory from Oracle 10g 4 25
Help with Oracle IF statment 5 26
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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 Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

839 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