gsl_rng__ranlux.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* rng/ranlux.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include "gsl_rng.h"
  22. /* This is a lagged fibonacci generator with skipping developed by Luescher.
  23. The sequence is a series of 24-bit integers, x_n,
  24. x_n = d_n + b_n
  25. where d_n = x_{n-10} - x_{n-24} - c_{n-1}, b_n = 0 if d_n >= 0 and
  26. b_n = 2^24 if d_n < 0, c_n = 0 if d_n >= 0 and c_n = 1 if d_n < 0,
  27. where after 24 samples a group of p integers are "skipped", to
  28. reduce correlations. By default p = 199, but can be increased to
  29. 365.
  30. The period of the generator is around 10^171.
  31. From: M. Luescher, "A portable high-quality random number generator
  32. for lattice field theory calculations", Computer Physics
  33. Communications, 79 (1994) 100-110.
  34. Available on the net as hep-lat/9309020 at http://xxx.lanl.gov/
  35. See also,
  36. F. James, "RANLUX: A Fortran implementation of the high-quality
  37. pseudo-random number generator of Luscher", Computer Physics
  38. Communications, 79 (1994) 111-114
  39. Kenneth G. Hamilton, F. James, "Acceleration of RANLUX", Computer
  40. Physics Communications, 101 (1997) 241-248
  41. Kenneth G. Hamilton, "Assembler RANLUX for PCs", Computer Physics
  42. Communications, 101 (1997) 249-253 */
  43. static inline unsigned long int ranlux_get (void *vstate);
  44. static double ranlux_get_double (void *vstate);
  45. static void ranlux_set_lux (void *state, unsigned long int s, unsigned int luxury);
  46. static void ranlux_set (void *state, unsigned long int s);
  47. static void ranlux389_set (void *state, unsigned long int s);
  48. static const unsigned long int mask_lo = 0x00ffffffUL; /* 2^24 - 1 */
  49. static const unsigned long int mask_hi = ~0x00ffffffUL;
  50. static const unsigned long int two24 = 16777216; /* 2^24 */
  51. typedef struct
  52. {
  53. unsigned int i;
  54. unsigned int j;
  55. unsigned int n;
  56. unsigned int skip;
  57. unsigned int carry;
  58. unsigned long int u[24];
  59. }
  60. ranlux_state_t;
  61. static inline unsigned long int increment_state (ranlux_state_t * state);
  62. static inline unsigned long int
  63. increment_state (ranlux_state_t * state)
  64. {
  65. unsigned int i = state->i;
  66. unsigned int j = state->j;
  67. long int delta = state->u[j] - state->u[i] - state->carry;
  68. if (delta & mask_hi)
  69. {
  70. state->carry = 1;
  71. delta &= mask_lo;
  72. }
  73. else
  74. {
  75. state->carry = 0;
  76. }
  77. state->u[i] = delta;
  78. if (i == 0)
  79. {
  80. i = 23;
  81. }
  82. else
  83. {
  84. i--;
  85. }
  86. state->i = i;
  87. if (j == 0)
  88. {
  89. j = 23;
  90. }
  91. else
  92. {
  93. j--;
  94. }
  95. state->j = j;
  96. return delta;
  97. }
  98. static inline unsigned long int
  99. ranlux_get (void *vstate)
  100. {
  101. ranlux_state_t *state = (ranlux_state_t *) vstate;
  102. const unsigned int skip = state->skip;
  103. unsigned long int r = increment_state (state);
  104. state->n++;
  105. if (state->n == 24)
  106. {
  107. unsigned int i;
  108. state->n = 0;
  109. for (i = 0; i < skip; i++)
  110. increment_state (state);
  111. }
  112. return r;
  113. }
  114. static double
  115. ranlux_get_double (void *vstate)
  116. {
  117. return ranlux_get (vstate) / 16777216.0;
  118. }
  119. static void
  120. ranlux_set_lux (void *vstate, unsigned long int s, unsigned int luxury)
  121. {
  122. ranlux_state_t *state = (ranlux_state_t *) vstate;
  123. int i;
  124. long int seed;
  125. if (s == 0)
  126. s = 314159265; /* default seed is 314159265 */
  127. seed = s;
  128. /* This is the initialization algorithm of F. James, widely in use
  129. for RANLUX. */
  130. for (i = 0; i < 24; i++)
  131. {
  132. unsigned long int k = seed / 53668;
  133. seed = 40014 * (seed - k * 53668) - k * 12211;
  134. if (seed < 0)
  135. {
  136. seed += 2147483563;
  137. }
  138. state->u[i] = seed % two24;
  139. }
  140. state->i = 23;
  141. state->j = 9;
  142. state->n = 0;
  143. state->skip = luxury - 24;
  144. if (state->u[23] & mask_hi)
  145. {
  146. state->carry = 1;
  147. }
  148. else
  149. {
  150. state->carry = 0;
  151. }
  152. }
  153. static void
  154. ranlux_set (void *vstate, unsigned long int s)
  155. {
  156. ranlux_set_lux (vstate, s, 223);
  157. }
  158. static void
  159. ranlux389_set (void *vstate, unsigned long int s)
  160. {
  161. ranlux_set_lux (vstate, s, 389);
  162. }
  163. static const gsl_rng_type ranlux_type =
  164. {"ranlux", /* name */
  165. 0x00ffffffUL, /* RAND_MAX */
  166. 0, /* RAND_MIN */
  167. sizeof (ranlux_state_t),
  168. &ranlux_set,
  169. &ranlux_get,
  170. &ranlux_get_double};
  171. static const gsl_rng_type ranlux389_type =
  172. {"ranlux389", /* name */
  173. 0x00ffffffUL, /* RAND_MAX */
  174. 0, /* RAND_MIN */
  175. sizeof (ranlux_state_t),
  176. &ranlux389_set,
  177. &ranlux_get,
  178. &ranlux_get_double};
  179. const gsl_rng_type *gsl_rng_ranlux = &ranlux_type;
  180. const gsl_rng_type *gsl_rng_ranlux389 = &ranlux389_type;