Re: Randomize in Forms 4.5

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: 1997/12/19
Message-ID: <349cd1f7.4734968_at_inet16>#1/1


On Fri, 19 Dec 1997 11:01:23 +0100, Edwin Smal <esmal_at_compuserve.com> wrote:

>Maybe any of you you can help me with a little problem.
>
>I'm trying to implement a 'randomizer' in Forms. Using date and time I
>try to generate a 'as random as possible' number. The only problem is
>that the number this program comes up with is not as random as i want.
>
>Think about it, will you?
>
>Thanks in advance.
>
>By the way; have a good christmas.
>
>Edwin Smal.

Here is one, I wrote it for the database, not forms. If you take out the pragmas, it'll probably compile in forms as well. It works just like the C "rand()" function (do a man on rand in unix to see the description)...

create or replace package random
is

        pragma restrict_references( random, WNDS, RNPS );

  • see the randon number generator. If you seed the
  • generator with the same value in 2 runs, it'll
  • generate the same set of 'random' numbers. procedure srand( new_seed in number );
  • returns an integer between 1 and 32k function rand return number; pragma restrict_references( rand, WNDS );
  • procedure version of the function above. procedure get_rand( r OUT number );
  • returns an integer in the set 1..N function rand_max( n IN number ) return number; pragma restrict_references( rand_max, WNDS);
  • procedure version of the function above. procedure get_rand_max( r OUT number, n IN number );

end random;
/

create or replace package body random
is

	multiplier	constant number         := 22695477;
	increment	constant number        	:= 1;
	"2^32"		constant number			:= 2 ** 32;
	"2^16"		constant number         := 2 ** 16;
	"0x7fff"	constant number         := 32767;
	Seed		number := 1;
--
	procedure srand( new_seed in number ) 
	is
	begin
		Seed := new_seed;
	end srand;
--
	function rand return number
	is
	begin
		seed := mod( multiplier * seed + increment, "2^32" );
		return bitand( seed/"2^16", "0x7fff" );
	end rand;
--
	procedure get_rand( r OUT number ) 
	is
	begin
		r := rand;
	end get_rand;
--
	function rand_max( n IN number ) return number 
	is
	begin
		return mod( rand, n ) + 1;
	end rand_max;
--
	procedure get_rand_max( r OUT number, n IN number ) 
	is
	begin
		r := rand_max( n );
	end get_rand_max;
--
begin
	select userenv( 'SESSIONID' ) into seed from dual;
end random;
/

 
Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government
Bethesda MD
 
http://govt.us.oracle.com/    -- downloadable utilities
 
----------------------------------------------------------------------------
Opinions are mine and do not necessarily reflect those of Oracle Corporation
 
Anti-Anti Spam Msg: if you want an answer emailed to you, 
you have to make it easy to get email to you.  Any bounced
email will be treated the same way i treat SPAM-- I delete it.
Received on Fri Dec 19 1997 - 00:00:00 CET

Original text of this message