home *** CD-ROM | disk | FTP | other *** search
- FUNCTION RANDOM: REAL;
-
- { This Turbo Pascal V4.0 function returns a REAL pseudo-random number
- in the range: 0.0 < RANDOM() < 1.0
-
- The period of this function is 2,147,483,646 (2**31-2); that is, it
- will generate 2,147,483,636 pseudo-random numbers before repeating
- the series.
-
- Examples:
- X := 2.0*RANDOM()-1.0;
- IF RANDOM < 0.5 THEN LOWER;
-
- In the first example, X is assigned a random number in the range
- of: -1.0 < X < +1.0. In the second example, procedure LOWER is
- called fifty percent of the time. (i.e. When the value returned by
- RANDOM is less than one-half.)
-
- Note: SEED, the random number seed, MUST be declared as a global
- variable of type LONGINT (i.e. An integer variable capable
- of representing numbers as large as 2,147,483,647) and MUST
- be initialized in the main procedure to a number in the
- range: 0 < SEED < 2147483647 before Function RANDOM is
- invoked. (Turbo Pascal allows this to be done using a
- "typed constant" as in: VAR SEED: LONGINT = 1; )
-
- This function is based on a Pascal routine given in the following
- reference:
-
- Park, Stephen K., & Miller, Keith W., "Random Number Generators:
- Good Ones are Hard to Find", in "Communications of the ACM", Vol. 31,
- No. 10 (October 1988), pp 1192 - 1201. Abstract: "Practical and
- theoretical issues are presented concerning the design, implementa-
- tion, and use of a good, minimal standard random number generator
- that will port to virtually all systems."
-
- Function coded by Harry M. Murphy on 31 October 1988. }
-
- CONST
- A = 16807; { The multiplier. }
- M = 2147483647; { The modulus = 2**31-1. }
- Q = 127773; { Q = M DIV A }
- R = 2836; { R = M MOD A }
-
- VAR
- LO, HI, TEMP : LONGINT;
-
- BEGIN
- HI := SEED DIV Q;
- LO := SEED MOD Q;
- TEMP := A*LO - R*HI;
- IF TEMP <= 0
- THEN
- SEED := TEMP + M
- ELSE
- SEED := TEMP;
- RANDOM := SEED / M
- END { Function RANDOM };