11.16 Random Number Generation


You use the DBMS_RANDOM package to generate random numbers . You set the seed with the SEED function, which is overloaded.

 
 PROCEDURE seed(val IN BINARY_INTEGER); PROCEDURE seed(val IN VARCHAR2); 

Two methods for setting a seed are:

 
 dbms_random.seed(123456); dbms_random.seed(TO_CHAR(SYSDATE,'dd-mon-yyyy hh24:mi:ss'); 

The function VALUE generates a 38-digit precision number within the range:

 
 0.0 <= value < 1.0 

The function definition for a random number is:

 
 FUNCTION value RETURN NUMBER; 

The following block illustrates the VALUE function.

 
 DECLARE     N NUMBER; BEGIN     dbms_random.seed(123456);     N := dbms_random.value;     -- N will be a number similar to:     --     -- 0.92531681298113309873787795771931592618 END; 

You can generate random numbers within a range. You call the function VALUE and pass the low and high limits. The function definition is:

 
 FUNCTION value(low IN NUMBER, high IN NUMBER) RETURN NUMBER; 

The random number returned is within the range:

 
 low <= value < high 

The following generates 10 random two-digit integer numbers.

 
 BEGIN     dbms_random.seed        (TO_CHAR(SYSDATE,'dd-mon-yyyy hh24:mi:ss'));     FOR i in 1..10 LOOP         dbms_output.put_line(TRUNC(dbms_random.value(10,100)));     END LOOP; END; 


Programming Oracle Triggers and Stored Procedures
Programming Oracle Triggers and Stored Procedures (3rd Edition) (Prentice Hall PTR Oracle Series)
ISBN: 0130850330
EAN: 2147483647
Year: 2003
Pages: 111
Authors: Kevin Owens

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net