home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / mac / programm / 18163 < prev    next >
Encoding:
Text File  |  1992-11-09  |  3.2 KB  |  122 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!tcsi.com!iat.holonet.net!bwilliam
  3. From: bwilliam@iat.holonet.net (Bill Williams)
  4. Subject: Re: looking for a REALLY RaNdOM "RANDOM"
  5. Message-ID: <BxFIJz.42H@iat.holonet.net>
  6. Organization: HoloNet (BBS: 510-704-1058)
  7. References: <1djhjiINNhlc@mthvax.cs.miami.edu>
  8. Date: Mon, 9 Nov 1992 03:19:10 GMT
  9. Lines: 111
  10.  
  11. Request for GOOD random number generator in Pascal:
  12.  
  13. I have lots of random number generator source, most are in "C" two are in
  14. 68000 assembly but one of them (My ultimate favorite) is actually in
  15. Pascal (The language you requested) here it is:
  16.  
  17.  
  18. ===========
  19.  
  20. UNIT Portable_Random_Numbers;
  21. INTERFACE
  22.  
  23.     FUNCTION Fetch_Random_Numbers: real;
  24.  
  25.     PROCEDURE Manually_Initialize_Seeds (value_1, value_2: LONGINT);
  26.  
  27.     PROCEDURE Auto_Initialize_Seeds;
  28.  
  29.  
  30. IMPLEMENTATION
  31.  
  32.     VAR
  33.         Random_Number_Seed_1, Random_Number_Seed_2: LONGINT; {
  34. signed 32-bit integers }
  35.  
  36.     FUNCTION Fetch_Random_Numbers: real;
  37.  
  38.         VAR
  39.             zz, kk: longint;
  40.  
  41.     BEGIN
  42.  
  43.         kk := Random_Number_Seed_1 DIV 53668;
  44.         Random_Number_Seed_1 := 40014 * (Random_Number_Seed_1 - kk
  45. * 53668) - kk * 12211;
  46.         IF Random_Number_Seed_1 < 0 THEN
  47.             Random_Number_Seed_1 := Random_Number_Seed_1 + 2147483563;
  48.  
  49.         kk := Random_Number_Seed_2 DIV 52774;
  50.         Random_Number_Seed_2 := 40692 * (Random_Number_Seed_2 - kk
  51. * 52774) - kk * 3791;
  52.         IF Random_Number_Seed_2 < 0 THEN
  53.             Random_Number_Seed_2 := Random_Number_Seed_2 + 2147483399;
  54.  
  55.         zz := Random_Number_Seed_1 - Random_Number_Seed_2;
  56.  
  57.         IF zz < 1 THEN
  58.             zz := zz + 2147483562;
  59.  
  60.         Fetch_Random_Numbers := zz * 4.65661E-10;
  61.  
  62.     END;
  63.  
  64.  
  65.     PROCEDURE Manually_Initialize_Seeds (value_1, value_2: LONGINT);
  66.  
  67. { This is for repeatable use of the Random number generator }
  68. { requirement is that  Random_Number_Seed_1 be in the range [1,
  69. 2_147_483_562] and  Random_Number_Seed_2 }
  70. { be in [1, 2_147_483_398] ( [ -2**31 + 85, 2**31 - 85 ]). }
  71.  
  72.     BEGIN
  73.         Random_Number_Seed_1 := value_1;
  74.         Random_Number_Seed_2 := value_2;
  75.     END;
  76.  
  77.  
  78.     PROCEDURE Auto_Initialize_Seeds;
  79.  
  80. { This version of Initialize_Seeds is Macintosh dependent. The theoretical }
  81. { requirement is that  Random_Number_Seed_1 be in the range [1,
  82. 2_147_483_562] and  Random_Number_Seed_2 }
  83. { be in [1, 2_147_483_398] ( [ -2**31 + 85, 2**31 - 85 ]). Here, 
  84. Random_Number_Seed_1 and }
  85. {  Random_Number_Seed_2 are in [0, 32_737]. See reference [1] cited in the
  86. comments of }
  87. { Fetch_Random_Numbers for more information on generating seeds. }
  88.  
  89.     BEGIN
  90.         GetDateTime(randSeed);
  91.         Random_Number_Seed_1 := abs(Random);
  92.         Random_Number_Seed_2 := abs(Random);
  93.     END;
  94.  
  95.  
  96.  
  97. END. { Portable_Random_Numbers }
  98. -------------
  99.  
  100.  
  101. It is a classic Multiplicative Linear Congruential Generator as known
  102. about for over 20 years and proven to be the best "shitty" safe and
  103. equally distributed, evenly repeating random technique.
  104.  
  105. references :
  106.  
  107. {1. L'Ecuyer, Pierre, Efficient and Portable Combined Random Number }
  108. { Generators. Communications of the ACM, 31, 6 (June 1988), 742-749, 774 }
  109.  
  110. L'Ecuyer reports that a comprehensive battery of over 20 different tests
  111. were performed on the generator that involved billions of psuedo-random
  112. numbers and over 200 hours of VAX-11/780 CPU time. The empirical behavior
  113. of the generators is very satisfactory for this class of generator.
  114.  
  115.  
  116. Hope that solves it!
  117.  
  118. Bill Williams
  119.  
  120.  
  121.  
  122.