Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: RANDOM NUMBER GENERATOR

Re: RANDOM NUMBER GENERATOR

From: Paul Russell <prussel6_at_removethis.ford.com>
Date: Thu, 9 Sep 1999 10:41:06 +0100
Message-ID: <7r7va7$d8k3@eccws1.dearborn.ford.com>


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
rem When no seed is provided, the package is originally initialized rem with the current user name and the time down to the current second. rem If this package is seeded twice with the same seed, then accessed rem in the same way, it will produce the same results in both cases.

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 distributed
value
      randval := randval*1e-38 + i*.01020304050607080910111213141516171819;
      mem(i)  := mod( randval, 1.0 );
      junk    := substr(junk,20);

    end loop;

    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;

    end loop;
  end init;

  function STRING (opt char, len number)     return varchar2 is -- string of <len> characters     optx char (1) := lower(opt);

    lo   number;
    rng  number;
    n    number;

    xstr varchar2 (60) := null;
  begin
    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 case
    end 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 string
    end 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 Zhang


Received on Thu Sep 09 1999 - 04:41:06 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US