Link to home
Start Free TrialLog in
Avatar of KG1973
KG1973

asked on

How to create random number using oracle ?

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.
Avatar of Naveen Kumar
Naveen Kumar
Flag of India image

ASKER CERTIFIED SOLUTION
Avatar of Sujith
Sujith
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KG1973
KG1973

ASKER

Perfect solution.
Avatar of KG1973

ASKER

You just solved my problem. Thank you very much.