runif.f 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. *DECK RUNIF
  2. FUNCTION RUNIF (T, N)
  3. C***BEGIN PROLOGUE RUNIF
  4. C***PURPOSE Generate a uniformly distributed random number.
  5. C***LIBRARY SLATEC (FNLIB)
  6. C***CATEGORY L6A21
  7. C***TYPE SINGLE PRECISION (RUNIF-S)
  8. C***KEYWORDS FNLIB, RANDOM NUMBER, SPECIAL FUNCTIONS, UNIFORM
  9. C***AUTHOR Fullerton, W., (LANL)
  10. C***DESCRIPTION
  11. C
  12. C This random number generator is portable among a wide variety of
  13. C computers. It generates a random number between 0.0 and 1.0 accord-
  14. C ing to the algorithm presented by Bays and Durham (TOMS, 2, 59,
  15. C 1976). The motivation for using this scheme, which resembles the
  16. C Maclaren-Marsaglia method, is to greatly increase the period of the
  17. C random sequence. If the period of the basic generator (RAND) is P,
  18. C then the expected mean period of the sequence generated by RUNIF is
  19. C given by new mean P = SQRT (PI*FACTORIAL(N)/(8*P)),
  20. C where FACTORIAL(N) must be much greater than P in this asymptotic
  21. C formula. Generally, N should be around 32 if P=4.E6 as for RAND.
  22. C
  23. C Input Argument --
  24. C N ABS(N) is the number of random numbers in an auxiliary table.
  25. C Note though that ABS(N)+1 is the number of items in array T.
  26. C If N is positive and differs from its value in the previous
  27. C invocation, then the table is initialized for the new value of
  28. C N. If N is negative, ABS(N) is the number of items in an
  29. C auxiliary table, but the tables are now assumed already to
  30. C be initialized. This option enables the user to save the
  31. C table T at the end of a long computer run and to restart with
  32. C the same sequence. Normally, RUNIF would be called at most
  33. C once with negative N. Subsequent invocations would have N
  34. C positive and of the correct magnitude.
  35. C
  36. C Input and Output Argument --
  37. C T an array of ABS(N)+1 random numbers from a previous invocation
  38. C of RUNIF. Whenever N is positive and differs from the old
  39. C N, the table is initialized. The first ABS(N) numbers are the
  40. C table discussed in the reference, and the N+1 -st value is Y.
  41. C This array may be saved in order to restart a sequence.
  42. C
  43. C Output Value --
  44. C RUNIF a random number between 0.0 and 1.0.
  45. C
  46. C***REFERENCES (NONE)
  47. C***ROUTINES CALLED RAND
  48. C***REVISION HISTORY (YYMMDD)
  49. C 770401 DATE WRITTEN
  50. C 890531 Changed all specific intrinsics to generic. (WRB)
  51. C 890531 REVISION DATE from Version 3.2
  52. C 891214 Prologue converted to Version 4.0 format. (BAB)
  53. C 910819 Added EXTERNAL statement for RAND due to problem on IBM
  54. C RS 6000. (WRB)
  55. C***END PROLOGUE RUNIF
  56. DIMENSION T(*)
  57. EXTERNAL RAND
  58. SAVE NOLD, FLOATN
  59. DATA NOLD /-1/
  60. C***FIRST EXECUTABLE STATEMENT RUNIF
  61. IF (N.EQ.NOLD) GO TO 20
  62. C
  63. NOLD = ABS(N)
  64. FLOATN = NOLD
  65. IF (N.LT.0) DUMMY = RAND (T(NOLD+1))
  66. IF (N.LT.0) GO TO 20
  67. C
  68. DO 10 I=1,NOLD
  69. T(I) = RAND (0.)
  70. 10 CONTINUE
  71. T(NOLD+1) = RAND (0.)
  72. C
  73. 20 J = T(NOLD+1)*FLOATN + 1.
  74. T(NOLD+1) = T(J)
  75. RUNIF = T(J)
  76. T(J) = RAND (0.)
  77. C
  78. RETURN
  79. END