Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: RANDOM NUMBER GENERATOR
Random number generator, courtesy of Steven Fuerstien:
rem Package RND: Generate random numbers
rem
rem This package should be installed as SYS. It generates a sequence of
rem random 38-digit Oracle numbers. The expected length of the sequence
rem is about power(10,28), which is hopefully long enough. The generator
rem used is called Delayed Fibonacci: m(i) := result = m(i)+m(i+24 mod
55).
rem
rem Numbers can be fetched through SQL statements, for example
rem
rem insert into a values (rnd.val); rem select rnd.val from dual; rem variable x number; rem execute :x := rnd.val; rem update a set a2=a2+1 where a1 < :x;rem
drop public synonym rnd
/
drop package rnd
/
create package rnd is
type num_array is table of number index by binary_integer;
end;
/
create package body rnd is
mem num_array; -- big internal state hidden from the
user
counter binary_integer; -- counter through the results other binary_integer; saved_norm number; -- unused random normally distributedvalue
randval := randval*1e-38 + i*.01020304050607080910111213141516171819; mem(i) := mod( randval, 1.0 ); junk := substr(junk,20);
for j in 0..10 loop
for i in 0..54 loop randval := mem(mod(i+55-1, 55)) * 1e24; randval := mod( randval, 1.0) + trunc(randval)*1e-38; randval := mem(i)+randval; if (randval >= 1.0) then randval := randval - 1.0; end if; mem(i) := randval; end loop;
function STRING (opt char, len number) return varchar2 is -- string of <len> characters optx char (1) := lower(opt);
lo number; rng number; n number;
if optx = 'u' then -- upper case alpha characters only lo := 65; rng := 26; -- ASCII 41 to 5A (hex) elsif optx = 'l' then -- lower case alpha characters only lo := 97; rng := 26; -- ASCII 61 to 7A (hex) elsif optx = 'a' then -- alpha characters only (mixed case) lo := 65; rng := 52; -- ASCII 41 to 5A and 61 to 7A (see below) elsif optx = 'x' then -- any alpha-numeric characters (upper) lo := 48; rng := 36; -- ASCII 30 to 39 and 41 to 5A (see below) elsif optx = 'p' then -- any printable characters lo := 32; rng := 95; -- ASCII 20 to 7E (hex) else lo := 65; rng := 26; -- default to upper caseend if;
for i in 1 .. least(len,60)
loop
/* Get random ASCII character value in specified range */ n := lo + trunc(rng * val); -- between lo and (lo + rng -1) /* Adjust for split range */ if optx = 'A' and n > 90 then n := n+6; -- exclude ASCII characters 5B to 60 elsif optx = 'X' and n > 57 then n := n+7; -- exclude ASCII characters 3A to 40 end if; xstr := xstr||chr(n); -- Append character to stringend loop;
return xstr;
end STRING;
begin
init(to_char(sysdate,'MM-DD-YYYY HH24:MI:SS')||user);
end;
/
create public synonym rnd for rnd
/
grant execute on rnd to public
/
Xiaofei Zhang <xz_at_liveoaktelecom.com> wrote in message news:37D6D2BE.6C04B2CB_at_liveoaktelecom.com...
> hey yall. > i am trynna write a random number generator, can i have some samples? > also, how do i display a line of text after i run it.(in SQL> )? > that is all, thanks for ur time > > -- > Xiaofei ZhangReceived on Thu Sep 09 1999 - 04:41:06 CDT