--***************************************************************************** -- -- Random Number Generators - Utils -- -- AUTHOR: Robert Walsh -- CLASS: CSCI 375 -- DATE: September 14, 1995 -- --***************************************************************************** with TEXT_IO; package INTEGER_IO is new TEXT_IO.INTEGER_IO(INTEGER); with TEXT_IO, INTEGER_IO; use TEXT_IO, INTEGER_IO; package Random is procedure InitRandom; -- WHAT: This procedure will prompt the user for an integer. The -- value of the integer will be used as the initial seed value of -- the random number generator. In the event that the user -- provides an unacceptable input, the value of the seed will be -- assigned 44449. The program notifies the user of the change. -- The procedure also initializes the randomness by calling the -- random number generator routine 15 times. -- IN: None -- OUT: None function RangeRand(Limit : INTEGER) return INTEGER; -- WHAT: This function creates a random number within a specified range. -- The number generated will be > 0 and <= Limit. Uses GetRand(). -- IN: The upper Limit [INTEGER] for the range of random numbers. -- OUT: A random number [INTEGER] of the specified range; not -- including zero. procedure GetRand(Rand : out FLOAT); -- WHAT: This procedure creates a positive random number [FLOAT] which -- is less than 1 including 0. Uses linear congruential algorithm -- IN: None -- OUT: Random number [FLOAT] >= 0 but < 1. end Random; Package body Random is Seed : INTEGER; -- Arbitrary number used to manipulate a random # procedure InitRandom is Dumb : FLOAT; -- Throw away random values begin PUT("Please enter a seed value... "); GET (Seed); for I in 1..15 loop GetRand(Dumb); -- gives the random routine a 'warm start' end loop; exception when others => PUT_LINE("Input error - Seed assigned value 44449"); Seed := 44449; -- If ANYTHING goes wrong: Seed = 44449 end InitRandom; function RangeRand(Limit : INTEGER) return INTEGER is Rand : FLOAT; -- holds random number < 1 and >= 0 begin GetRand(Rand); -- Base for random number return INTEGER(Rand*FLOAT(Limit-1))+1; -- Compute random range value end RangeRand; procedure GetRand(Rand : out FLOAT) is Divider, Multiplier, Adder : INTEGER; -- Divider, Multiplier, Adder are values used in creating a random # begin Divider := 65535; -- 2^n on n bit machine Multiplier := 15625; -- Arbitrary add int < Divider Adder := 22221; -- Arbitrary odd int < Divider Seed := (Multiplier * Seed + Adder) mod Divider; Rand := FLOAT(Seed) / FLOAT(Divider - 1); exception when others => -- usually constraint error generated by initial input -- we will fix by reseting Seed Seed := 44449; -- if error occurs, reset random routine end GetRand; begin null; -- Hey, this pakage is only simple random number subroutines end Random;