home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!tcsi.com!iat.holonet.net!bwilliam
- From: bwilliam@iat.holonet.net (Bill Williams)
- Subject: Re: looking for a REALLY RaNdOM "RANDOM"
- Message-ID: <BxFIJz.42H@iat.holonet.net>
- Organization: HoloNet (BBS: 510-704-1058)
- References: <1djhjiINNhlc@mthvax.cs.miami.edu>
- Date: Mon, 9 Nov 1992 03:19:10 GMT
- Lines: 111
-
- Request for GOOD random number generator in Pascal:
-
- I have lots of random number generator source, most are in "C" two are in
- 68000 assembly but one of them (My ultimate favorite) is actually in
- Pascal (The language you requested) here it is:
-
-
- ===========
-
- UNIT Portable_Random_Numbers;
- INTERFACE
-
- FUNCTION Fetch_Random_Numbers: real;
-
- PROCEDURE Manually_Initialize_Seeds (value_1, value_2: LONGINT);
-
- PROCEDURE Auto_Initialize_Seeds;
-
-
- IMPLEMENTATION
-
- VAR
- Random_Number_Seed_1, Random_Number_Seed_2: LONGINT; {
- signed 32-bit integers }
-
- FUNCTION Fetch_Random_Numbers: real;
-
- VAR
- zz, kk: longint;
-
- BEGIN
-
- kk := Random_Number_Seed_1 DIV 53668;
- Random_Number_Seed_1 := 40014 * (Random_Number_Seed_1 - kk
- * 53668) - kk * 12211;
- IF Random_Number_Seed_1 < 0 THEN
- Random_Number_Seed_1 := Random_Number_Seed_1 + 2147483563;
-
- kk := Random_Number_Seed_2 DIV 52774;
- Random_Number_Seed_2 := 40692 * (Random_Number_Seed_2 - kk
- * 52774) - kk * 3791;
- IF Random_Number_Seed_2 < 0 THEN
- Random_Number_Seed_2 := Random_Number_Seed_2 + 2147483399;
-
- zz := Random_Number_Seed_1 - Random_Number_Seed_2;
-
- IF zz < 1 THEN
- zz := zz + 2147483562;
-
- Fetch_Random_Numbers := zz * 4.65661E-10;
-
- END;
-
-
- PROCEDURE Manually_Initialize_Seeds (value_1, value_2: LONGINT);
-
- { This is for repeatable use of the Random number generator }
- { requirement is that Random_Number_Seed_1 be in the range [1,
- 2_147_483_562] and Random_Number_Seed_2 }
- { be in [1, 2_147_483_398] ( [ -2**31 + 85, 2**31 - 85 ]). }
-
- BEGIN
- Random_Number_Seed_1 := value_1;
- Random_Number_Seed_2 := value_2;
- END;
-
-
- PROCEDURE Auto_Initialize_Seeds;
-
- { This version of Initialize_Seeds is Macintosh dependent. The theoretical }
- { requirement is that Random_Number_Seed_1 be in the range [1,
- 2_147_483_562] and Random_Number_Seed_2 }
- { be in [1, 2_147_483_398] ( [ -2**31 + 85, 2**31 - 85 ]). Here,
- Random_Number_Seed_1 and }
- { Random_Number_Seed_2 are in [0, 32_737]. See reference [1] cited in the
- comments of }
- { Fetch_Random_Numbers for more information on generating seeds. }
-
- BEGIN
- GetDateTime(randSeed);
- Random_Number_Seed_1 := abs(Random);
- Random_Number_Seed_2 := abs(Random);
- END;
-
-
-
- END. { Portable_Random_Numbers }
- -------------
-
-
- It is a classic Multiplicative Linear Congruential Generator as known
- about for over 20 years and proven to be the best "shitty" safe and
- equally distributed, evenly repeating random technique.
-
- references :
-
- {1. L'Ecuyer, Pierre, Efficient and Portable Combined Random Number }
- { Generators. Communications of the ACM, 31, 6 (June 1988), 742-749, 774 }
-
- L'Ecuyer reports that a comprehensive battery of over 20 different tests
- were performed on the generator that involved billions of psuedo-random
- numbers and over 200 hours of VAX-11/780 CPU time. The empirical behavior
- of the generators is very satisfactory for this class of generator.
-
-
- Hope that solves it!
-
- Bill Williams
-
-
-
-