home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmos2002.zip / SRC / RANDCARD.MOD < prev    next >
Text File  |  1996-06-25  |  3KB  |  67 lines

  1. IMPLEMENTATION MODULE RandCard;
  2.  
  3.         (********************************************************)
  4.         (*                                                      *)
  5.         (*              Random number generator                 *)
  6.         (*                                                      *)
  7.         (*  Programmer:         P. Moylan                       *)
  8.         (*  Last edited:        25 June 1996                    *)
  9.         (*  Status:             Working                         *)
  10.         (*                                                      *)
  11.         (********************************************************)
  12.  
  13. (************************************************************************)
  14. (*                                                                      *)
  15. (* The algorithm used is Schrage's method, as described in              *)
  16. (*      Stephen K. Park and Keith W. Miller, "Random Number Generators: *)
  17. (*      Good ones are hard to find", CACM 31(10), Oct 1988, 1192-1201.  *)
  18. (* A basic property of this particular implementation is that all       *)
  19. (* intermediate results fit into 32 bits (including sign).              *)
  20. (*                                                                      *)
  21. (************************************************************************)
  22.  
  23. CONST a = 16807;                  (* 7^5 *)
  24.  
  25. (************************************************************************)
  26.  
  27. PROCEDURE RandCardinal (): CARDINAL;
  28.  
  29.     (* Returns a random number in the range [1..modulus-1],     *)
  30.     (* with a uniform distribution over that range.             *)
  31.  
  32.     CONST r = modulus MOD a;    (* 2836 *)
  33.           q = modulus DIV a;    (* 127773 *)
  34.  
  35.     VAR high, low: CARDINAL;  test: INTEGER;
  36.  
  37.     BEGIN
  38.         high := seed DIV q;  low := seed MOD q;
  39.         test := VAL(INTEGER,a*low) - VAL(INTEGER,r*high);
  40.         IF test > 0 THEN seed := test
  41.         ELSE seed := test + modulus;
  42.         END (*IF*);
  43.         RETURN seed;
  44.     END RandCardinal;
  45.  
  46. (****************************************************************)
  47.  
  48. PROCEDURE RandInt (min, max: INTEGER): INTEGER;
  49.  
  50.     (* Returns a random number in the range [min..max]          *)
  51.     (* (inclusive), with an approximately uniform distribution  *)
  52.     (* over that range.  (In this version I'm not being fussy   *)
  53.     (* about the precise distribution.)                         *)
  54.  
  55.     BEGIN
  56.         RETURN VAL(INTEGER,RandCardinal()) MOD (max-min) + min + 1;
  57.     END RandInt;
  58.  
  59. (****************************************************************)
  60. (*                      MODULE INITIALISATION                   *)
  61. (****************************************************************)
  62.  
  63. BEGIN
  64.     seed := 1;
  65. END RandCard.
  66. 
  67.