home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!psgrain!puddle!Greg.Vigneault
- From: Greg.Vigneault@puddle.fidonet.org (Greg Vigneault)
- Newsgroups: comp.lang.modula2
- Subject: RANDOM NUMBERS
- Message-ID: <41432.2B62F5AF@puddle.fidonet.org>
- Date: 22 Jan 93 22:03:11 GMT
- Article-I.D.: puddle.41432.2B62F5AF
- Organization: Pacific Systems Group
- Lines: 82
-
- In message-ID <m0nCnz6-000QziC@aton.abo.fi>
- skronkvi@aton.abo.fi (Stefan Kronkvist INF) writes...
-
- SK> Does anybody have a programme,procedure or module that returns
- > random numbers in Modula-2 (numbers from 0-9 are needed).
-
- The following was posted in the Pascal newsgroup, some months
- ago. You should be able to adapt it to Modula-2 ...
-
- (*********************************************************************)
- 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. }
-
- {Changed 10 August 1992 by Edward Lee. Eliminated TEMP variable.
- Utilized Turbo Pascal v4.0's predeclared RandSeed instead of SEED.
- RandSeed can be initialized by the system clock by calling the Turbo
- Pascal function Randomize}
-
- 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
- LOW, HIGH : LongInt;
-
- Begin
- HIGH := RandSeed Div Q;
- LOW := RandSeed Mod Q;
- RandSeed := A * LOW - R * HIGH;
- If RandSeed <= 0 Then
- RandSeed := RandSeed + M;
- Random := RandSeed / M;
- End; { Function RANDOM }
-
- (*********************************************************************)
-
- Greg Vigneault
-
- Jan.22.1993.Toronto.Canada.
- greg.vigneault@bville.gts.org
- gregsv@eastern.com
-
- --
- uucp: uunet!m2xenix!puddle!Greg.Vigneault
- Internet: Greg.Vigneault@puddle.fidonet.org
-