home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / random / randoms / random.for < prev    next >
Encoding:
Text File  |  1988-10-31  |  3.6 KB  |  118 lines

  1.       FUNCTION RANDOM()
  2.  
  3. C     This FORTRAN-77 function returns a REAL  pseudo-random  number  in
  4. C     the range:  0.0 < RANDOM() < 1.0
  5.  
  6. C     The period of this function is  2,147,483,646  (2**31-2);  that is,
  7. C     it will generate 2,147,483,636 pseudo-random numbers before repeat-
  8. C     ing the series.
  9.  
  10. C     The seed for this generator can  be  set  by:   CALL SETRAN(SEED),
  11. C     where SEED is an INTEGER in the range:   0 < SEED < 2,147,483,647;
  12. C     the default starting seed is 1.
  13.  
  14. C     Examples:
  15. C                   X = 2.0*RANDOM()-1.0
  16. C                   IF (RANDOM().LT.0.5) CALL LOWER
  17.  
  18. C     In the first example,  X is assigned a random number in the  range
  19. C     of:  -1.0 < X < +1.0.  In the second example,  subroutine LOWER is
  20. C     called fifty percent of the time.  (i.e.  When the value  returned
  21. C     by RANDOM() is less than one-half.)
  22.  
  23. C     Notes:
  24.  
  25. C             (1)  This function MUST be compiled and run on a  computer
  26. C             which supports 32-bit, or larger, integers.
  27.  
  28. C             (2)  This function uses BLOCK DATA RANSEE  to  define  the
  29. C             starting value for SEED.   If the function is not compiled
  30. C             with BLOCK DATA RANSEE, the user MUST initialize SEED by a
  31. C             call to SETRAN.
  32.  
  33. C     This function is based on a Pascal routine given in the  following
  34. C     reference:
  35.  
  36. C     Park, Stephen K., &  Miller, Keith W.,  "Random Number Generators:
  37. C     Good Ones are Hard to Find", in "Communications of the ACM",  Vol.
  38. C     31, No. 10 (October 1988),  pp 1192 - 1201.  Abstract:  "Practical
  39. C     and theoretical issues are presented concerning the design, imple-
  40. C     mentation, and use of a good, minimal standard random number  gen-
  41. C     erator that will port to virtually all systems."
  42.  
  43. C     Function coded by Harry M. Murphy on 31 October 1988.
  44.  
  45.       INTEGER A, HI, LO, M, Q, R, SEED, TEMP
  46.  
  47.       REAL RANDOM
  48.  
  49.       COMMON /RANCOM/ SEED
  50.  
  51.       SAVE /RANCOM/
  52.  
  53. C     A = the multiplier.  M = the modulus = 2**31-1.
  54. C     Q = M div A.         R = M mod A.
  55.  
  56.       PARAMETER (A = 16807, M = 2147483647, Q = 127773, R = 2836)
  57.  
  58. C                   Execution of FUNCTION RANDOM() starts here.
  59.       HI = SEED / Q
  60.       LO = MOD(SEED,Q)
  61.       TEMP = A*LO - R*HI
  62.       IF (TEMP.LE.0)
  63.      :  THEN
  64.           SEED = TEMP + M
  65.         ELSE
  66.           SEED = TEMP
  67.         END IF
  68.       RANDOM = REAL(SEED) / M
  69.       RETURN
  70.       END
  71.       SUBROUTINE SETRAN(SD)
  72.  
  73. C     Subroutine SETRAN initializes the random  number  seed,  SEED,  in
  74. C     common block /RANCOM/, based on the INTEGER argument, SD, where SD
  75. C     is in the range:  0 < SD < 2,147,483,647 .
  76.  
  77. C     SEED is used by the random number generator, FUNCTION RANDOM().
  78.  
  79. C     SEED is defined in BLOCK DATA RANSEE.
  80.  
  81. C     Subroutine coded by Harry M. Murphy on 31 October 1988.
  82.  
  83.       INTEGER MD, SD, SEED
  84.  
  85.       COMMON /RANCOM/ SEED
  86.  
  87.       SAVE /RANCOM/
  88.  
  89.       PARAMETER (MD = 2147483647)
  90.  
  91. C                   Execution of SETRAN starts here.
  92. C                   If SD is in range, set SEED = SD,
  93. C                   otherwise, set SEED = 1.
  94.       IF ((SD.GT.0).AND.(SD.LT.MD))
  95.      :  THEN
  96.           SEED = SD
  97.         ELSE
  98.           SD = 1
  99.         END IF
  100.       RETURN
  101.       END
  102.       BLOCK DATA RANSEE
  103.  
  104. C     This block data routine defines the initial value of SEED, used in
  105. C     the pseudo-random number generator, FUNCTION RANDOM().
  106.  
  107. C     Coded by Harry M. Murphy on 31 October 1988.
  108.  
  109.       INTEGER SEED
  110.  
  111.       COMMON /RANCOM/ SEED
  112.  
  113.       SAVE /RANDOM/
  114.  
  115.       DATA SEED/1/
  116.  
  117.       END
  118.