home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / modula2 / 1734 < prev    next >
Encoding:
Internet Message Format  |  1993-01-24  |  3.2 KB

  1. Path: sparky!uunet!ogicse!psgrain!puddle!Greg.Vigneault
  2. From: Greg.Vigneault@puddle.fidonet.org (Greg Vigneault)
  3. Newsgroups: comp.lang.modula2
  4. Subject: RANDOM NUMBERS
  5. Message-ID: <41432.2B62F5AF@puddle.fidonet.org>
  6. Date: 22 Jan 93 22:03:11 GMT
  7. Article-I.D.: puddle.41432.2B62F5AF
  8. Organization: Pacific Systems Group
  9. Lines: 82
  10.  
  11. In message-ID <m0nCnz6-000QziC@aton.abo.fi>
  12. skronkvi@aton.abo.fi (Stefan Kronkvist INF) writes...
  13.  
  14. SK> Does anybody have a programme,procedure or module that returns
  15.   > random numbers in Modula-2 (numbers from 0-9 are needed).
  16.  
  17.  The following was posted in the Pascal newsgroup, some months
  18.  ago.  You should be able to adapt it to Modula-2 ...
  19.  
  20. (*********************************************************************)
  21. Function Random : Real;
  22.  
  23. {This Turbo Pascal V4.0 function returns a REAL pseudo-random number
  24.  in the range:  0.0 < RANDOM() < 1.0
  25.  
  26.  The period of this function is  2,147,483,646 (2**31-2);  that is, it
  27.  will generate 2,147,483,636 pseudo-random  numbers  before  repeating
  28.  the series.
  29.  
  30.  Examples:
  31.                    X := 2.0*RANDOM()-1.0;
  32.                    IF RANDOM < 0.5 THEN LOWER;
  33.  
  34. In the first example,  X is assigned a random  number  in  the  range
  35. of:  -1.0 < X < +1.0.   In the second  example,  procedure  LOWER  is
  36. called fifty percent of the time.  (i.e. When the value  returned  by
  37. RANDOM is less than one-half.)
  38.  
  39. Note:  SEED, the random number seed, MUST be declared as a  global
  40.        variable of type LONGINT  (i.e. An integer variable capable
  41.        of representing numbers as large as 2,147,483,647) and MUST
  42.        be initialized in the main procedure to  a  number  in  the
  43.        range:  0 < SEED < 2147483647  before  Function  RANDOM  is
  44.        invoked.   (Turbo Pascal allows this to  be  done  using  a
  45.        "typed constant" as in:  VAR  SEED: LONGINT = 1; )
  46.  
  47.  This function is based on a Pascal routine  given  in  the  following
  48.  reference:
  49.  
  50.  Park, Stephen K.,  &  Miller, Keith W.,  "Random  Number  Generators:
  51.  Good Ones are Hard to Find", in "Communications of the ACM", Vol. 31,
  52.  No. 10  (October 1988),  pp 1192 - 1201.    Abstract:  "Practical and
  53.  theoretical issues are presented concerning the  design,  implementa-
  54.  tion,  and use of a good,  minimal standard random  number  generator
  55.  that will port to virtually all systems."
  56.  
  57.  Function coded by Harry M. Murphy on 31 October 1988.  }
  58.  
  59. {Changed 10 August 1992 by Edward Lee. Eliminated TEMP variable.
  60.  Utilized Turbo Pascal v4.0's predeclared RandSeed instead of SEED.
  61.  RandSeed can be initialized by the system clock by calling the Turbo
  62.  Pascal function Randomize}
  63.  
  64. Const
  65.   A = 16807;       { The multiplier.         }
  66.   M = 2147483647;  { The modulus = 2**31-1.  }
  67.   Q = 127773;      { Q = M DIV A             }
  68.   R = 2836;        { R = M MOD A             }
  69.  
  70. Var
  71.   LOW, HIGH : LongInt;
  72.  
  73. Begin
  74.   HIGH := RandSeed Div Q;
  75.   LOW := RandSeed Mod Q;
  76.   RandSeed := A * LOW - R * HIGH;
  77.   If RandSeed <= 0 Then
  78.      RandSeed := RandSeed + M;
  79.   Random := RandSeed / M;
  80. End; { Function RANDOM }
  81.  
  82. (*********************************************************************)
  83.  
  84.  Greg Vigneault
  85.  
  86.  Jan.22.1993.Toronto.Canada.
  87.  greg.vigneault@bville.gts.org
  88.  gregsv@eastern.com
  89.  
  90. --  
  91. uucp: uunet!m2xenix!puddle!Greg.Vigneault
  92. Internet: Greg.Vigneault@puddle.fidonet.org
  93.