Re: Randomize in Forms 4.5
Date: 1997/12/22
Message-ID: <349e1442.1405120_at_news-ath.forthnet.gr>#1/1
Good morning, Edwin.
I 'm sending you a package which solves your problem.
It generates random numbers between 1..32767.
I have been given it by Oracle.
I hope it helps.
CREATE OR REPLACE PACKAGE Random AS
/* Random number generator. Uses the same algorithm as the
rand() function in C. */
- Used to change the seed. From a given seed, the same
- sequence of random numbers will be generated. PROCEDURE ChangeSeed(p_NewSeed IN NUMBER);
- Returns a random integer between 1 and 32767. FUNCTION Rand RETURN NUMBER; PRAGMA RESTRICT_REFERENCES(rand, WNDS, WNPS, RNPS);
- Same as Rand, but with a procedural interface. PROCEDURE GetRand(p_RandomNumber OUT NUMBER);
- Returns a random integer between 1 and p_MaxVal. FUNCTION RandMax(p_MaxVal IN NUMBER) RETURN NUMBER; PRAGMA RESTRICT_REFERENCES(RandMax, WNDS);
- Same as RandMax, but with a procedural interface. PROCEDURE GetRandMax(p_RandomNumber OUT NUMBER, p_MaxVal IN NUMBER); END Random; / CREATE OR REPLACE PACKAGE BODY Random AS /* Used for calculating the next number. */ v_Multiplier CONSTANT NUMBER := 22695477; v_Increment CONSTANT NUMBER := 1;
/* Seed used to generate random sequence. */
v_Seed NUMBER := 1;
PROCEDURE ChangeSeed(p_NewSeed IN NUMBER) IS
BEGIN
v_Seed := p_NewSeed;
END ChangeSeed;
FUNCTION Rand RETURN NUMBER IS
BEGIN
v_Seed := MOD(v_Multiplier * v_Seed + v_Increment, (2 ** 32)); RETURN BITAND(v_Seed/(2 ** 16), 32767);END Rand;
PROCEDURE GetRand(p_RandomNumber OUT NUMBER) IS BEGIN
- Simply call Rand and return the value. p_RandomNumber := Rand; END GetRand;
FUNCTION RandMax(p_MaxVal IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN MOD(Rand, p_MaxVal) + 1;
END RandMax;
PROCEDURE GetRandMax(p_RandomNumber OUT NUMBER,
p_MaxVal IN NUMBER) IS BEGIN -- Simply call RandMax and return the value. p_RandomNumber := RandMax(p_MaxVal);END GetRandMax;
BEGIN
/* Package initialization. Initialize the seed to the current
time in seconds. */
ChangeSeed(TO_NUMBER(TO_CHAR(SYSDATE, 'SSSSS')));
END Random;
/
--In order to retrieve a random number, you can simply call
Random.Rand. The
--sequence of random numbers is controlled by the initial seed -- the
same
--sequence is generated for a given seed. Thus, in order to provide
more random
--values, we need to initialize the seed to a different value each
time the
--package is instantiated. This is accomplished with a call to the
ChangeSeed
--procedure from the package initialization section.
--This excerpt is from the "Packages: Package Initialization" section
--in Chapter 5: Procedures, Functions, Packages, and Triggers
--of Oracle PL/SQL Programming by Scott Urman
--published by the Oracle Press.
Merry Christmas And Happy new year.
Regards,
Vagelis Rados
Singular Computer Applications
mail to : vrados_at_singular.gr
Web Site : http://www.singular.gr Received on Mon Dec 22 1997 - 00:00:00 CET