Random string generator

If you’ve ever wanted to generate a random password, a random number, or just a random string, dbms_random is here to help. Here is an example of the different uses of this useful package:

BEGIN
/*
To initialize or reset the generator, 
call the seed procedure. Seed with a 
binary integer
*/
dbms_random.seed(1234535678);

/*
Get a random 38-digit precision 
number, 0.0 <= value < 1.0
*/
dbms_output.put_line(
    TO_CHAR(dbms_random.value)
    );

/*
get a random real number between 1 and 5
*/
dbms_output.put_line(
    TO_CHAR(dbms_random.VALUE(1,5))
    );

/*
get a random integer between 1 and 100
*/
dbms_output.put_line(
    TO_CHAR(TRUNC(dbms_random.VALUE(1,100)))
    );

/*
get a random string.
First parameter:
'u','U' : upper case alpha characters only
'l','L' : lower case alpha characters only
'a','A' : alpha characters only (mixed case)
'x','X' : any alpha-numeric characters (upper)
'p','P' : any printable characters
Second Parameter: Length (max 60)
*/
dbms_output.put_line(
    dbms_random.string('p',8)
    );

/*
Seed with a string (up to length 2000)
*/
dbms_random.seed(
    TO_CHAR(SYSDATE,'MM-DD-YYYY HH24:MI:SS')
    );

/*
get a random integer between 1 and 100
*/
dbms_output.put_line(
    TO_CHAR(TRUNC(dbms_random.VALUE(1,100)))
    );

END;


Possibly related:


Tagged | Post a Comment