home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mod201j.zip / modula2.exe / os2src / randoms.mod < prev    next >
Text File  |  1996-01-28  |  782b  |  39 lines

  1. (*
  2.   Programming Xinix;
  3.   Johan terryn 1996;
  4.   Multiplicative linear congruential algorithm : D.H.Lehmer;
  5.   Mathematical details : Park S.K. and Miller K.W. (1998) Random number generators, good ones are hard to find. Communications of the ACM, 31, 1192-1201.;
  6.   Returns a pseudo randomnumber between 0 <  and  <= 1;
  7.   Very fast method.
  8. *)
  9. IMPLEMENTATION MODULE Randoms;
  10.  
  11. VAR Z: LONGINT;
  12.  
  13. PROCEDURE Random(): REAL;
  14. CONST a = 16807;
  15.       m = 2147483647;
  16.       q = (m DIV a);
  17.       r = (m MOD a);
  18. VAR g: LONGINT;
  19. BEGIN
  20.   g := a*(Z MOD q) - r*(Z DIV q);
  21.   IF g > 0 THEN
  22.     Z := g
  23.   ELSE
  24.     Z := g + m
  25.  END;
  26.  RETURN FLOAT(Z)/FLOAT(m)
  27. END Random;
  28.  
  29.  
  30. PROCEDURE InitSeed(seed: LONGINT);
  31. BEGIN
  32.   Z := seed
  33. END InitSeed;
  34.  
  35. BEGIN
  36.  Z := 1;
  37. END Randoms.
  38.  
  39.