You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

slaran.f 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. *> \brief \b SLARAN
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. * Definition:
  9. * ===========
  10. *
  11. * REAL FUNCTION SLARAN( ISEED )
  12. *
  13. * .. Array Arguments ..
  14. * INTEGER ISEED( 4 )
  15. * ..
  16. *
  17. *
  18. *> \par Purpose:
  19. * =============
  20. *>
  21. *> \verbatim
  22. *>
  23. *> SLARAN returns a random real number from a uniform (0,1)
  24. *> distribution.
  25. *> \endverbatim
  26. *
  27. * Arguments:
  28. * ==========
  29. *
  30. *> \param[in,out] ISEED
  31. *> \verbatim
  32. *> ISEED is INTEGER array, dimension (4)
  33. *> On entry, the seed of the random number generator; the array
  34. *> elements must be between 0 and 4095, and ISEED(4) must be
  35. *> odd.
  36. *> On exit, the seed is updated.
  37. *> \endverbatim
  38. *
  39. * Authors:
  40. * ========
  41. *
  42. *> \author Univ. of Tennessee
  43. *> \author Univ. of California Berkeley
  44. *> \author Univ. of Colorado Denver
  45. *> \author NAG Ltd.
  46. *
  47. *> \ingroup real_matgen
  48. *
  49. *> \par Further Details:
  50. * =====================
  51. *>
  52. *> \verbatim
  53. *>
  54. *> This routine uses a multiplicative congruential method with modulus
  55. *> 2**48 and multiplier 33952834046453 (see G.S.Fishman,
  56. *> 'Multiplicative congruential random number generators with modulus
  57. *> 2**b: an exhaustive analysis for b = 32 and a partial analysis for
  58. *> b = 48', Math. Comp. 189, pp 331-344, 1990).
  59. *>
  60. *> 48-bit integers are stored in 4 integer array elements with 12 bits
  61. *> per element. Hence the routine is portable across machines with
  62. *> integers of 32 bits or more.
  63. *> \endverbatim
  64. *>
  65. * =====================================================================
  66. REAL FUNCTION SLARAN( ISEED )
  67. *
  68. * -- LAPACK auxiliary routine --
  69. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  70. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  71. *
  72. * .. Array Arguments ..
  73. INTEGER ISEED( 4 )
  74. * ..
  75. *
  76. * =====================================================================
  77. *
  78. * .. Parameters ..
  79. INTEGER M1, M2, M3, M4
  80. PARAMETER ( M1 = 494, M2 = 322, M3 = 2508, M4 = 2549 )
  81. REAL ONE
  82. PARAMETER ( ONE = 1.0E+0 )
  83. INTEGER IPW2
  84. REAL R
  85. PARAMETER ( IPW2 = 4096, R = ONE / IPW2 )
  86. * ..
  87. * .. Local Scalars ..
  88. INTEGER IT1, IT2, IT3, IT4
  89. REAL RNDOUT
  90. * ..
  91. * .. Intrinsic Functions ..
  92. INTRINSIC MOD, REAL
  93. * ..
  94. * .. Executable Statements ..
  95. 10 CONTINUE
  96. *
  97. * multiply the seed by the multiplier modulo 2**48
  98. *
  99. IT4 = ISEED( 4 )*M4
  100. IT3 = IT4 / IPW2
  101. IT4 = IT4 - IPW2*IT3
  102. IT3 = IT3 + ISEED( 3 )*M4 + ISEED( 4 )*M3
  103. IT2 = IT3 / IPW2
  104. IT3 = IT3 - IPW2*IT2
  105. IT2 = IT2 + ISEED( 2 )*M4 + ISEED( 3 )*M3 + ISEED( 4 )*M2
  106. IT1 = IT2 / IPW2
  107. IT2 = IT2 - IPW2*IT1
  108. IT1 = IT1 + ISEED( 1 )*M4 + ISEED( 2 )*M3 + ISEED( 3 )*M2 +
  109. $ ISEED( 4 )*M1
  110. IT1 = MOD( IT1, IPW2 )
  111. *
  112. * return updated seed
  113. *
  114. ISEED( 1 ) = IT1
  115. ISEED( 2 ) = IT2
  116. ISEED( 3 ) = IT3
  117. ISEED( 4 ) = IT4
  118. *
  119. * convert 48-bit integer to a real number in the interval (0,1)
  120. *
  121. RNDOUT = R*( REAL( IT1 )+R*( REAL( IT2 )+R*( REAL( IT3 )+R*
  122. $ ( REAL( IT4 ) ) ) ) )
  123. *
  124. IF (RNDOUT.EQ.1.0) THEN
  125. * If a real number has n bits of precision, and the first
  126. * n bits of the 48-bit integer above happen to be all 1 (which
  127. * will occur about once every 2**n calls), then SLARAN will
  128. * be rounded to exactly 1.0. In IEEE single precision arithmetic,
  129. * this will happen relatively often since n = 24.
  130. * Since SLARAN is not supposed to return exactly 0.0 or 1.0
  131. * (and some callers of SLARAN, such as CLARND, depend on that),
  132. * the statistically correct thing to do in this situation is
  133. * simply to iterate again.
  134. * N.B. the case SLARAN = 0.0 should not be possible.
  135. *
  136. GOTO 10
  137. END IF
  138. *
  139. SLARAN = RNDOUT
  140. RETURN
  141. *
  142. * End of SLARAN
  143. *
  144. END