gsl_rng__gfsr4.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* This program is free software; you can redistribute it and/or
  2. modify it under the terms of the GNU General Public License as
  3. published by the Free Software Foundation; either version 3 of the
  4. License, or (at your option) any later version.
  5. This program is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. General Public License for more details. You should have received
  9. a copy of the GNU General Public License along with this program;
  10. if not, write to the Free Foundation, Inc., 59 Temple Place, Suite
  11. 330, Boston, MA 02111-1307 USA
  12. From Robert M. Ziff, "Four-tap shift-register-sequence
  13. random-number generators," Computers in Physics 12(4), Jul/Aug
  14. 1998, pp 385-392. A generalized feedback shift-register (GFSR)
  15. is basically an xor-sum of particular past lagged values. A
  16. four-tap register looks like:
  17. ra[nd] = ra[nd-A] ^ ra[nd-B] ^ ra[nd-C] ^ ra[nd-D]
  18. Ziff notes that "it is now widely known" that two-tap registers
  19. have serious flaws, the most obvious one being the three-point
  20. correlation that comes from the defn of the generator. Nice
  21. mathematical properties can be derived for GFSR's, and numerics
  22. bears out the claim that 4-tap GFSR's with appropriately chosen
  23. offsets are as random as can be measured, using the author's test.
  24. This implementation uses the values suggested the the author's
  25. example on p392, but altered to fit the GSL framework. The "state"
  26. is 2^14 longs, or 64Kbytes; 2^14 is the smallest power of two that
  27. is larger than D, the largest offset. We really only need a state
  28. with the last D values, but by going to a power of two, we can do a
  29. masking operation instead of a modulo, and this is presumably
  30. faster, though I haven't actually tried it. The article actually
  31. suggested a short/fast hack:
  32. #define RandomInteger (++nd, ra[nd&M]=ra[(nd-A)&M]\
  33. ^ra[(nd-B)&M]^ra[(nd-C)&M]^ra[(nd-D)&M])
  34. so that (as long as you've defined nd,ra[M+1]), then you ca do things
  35. like: 'if (RandomInteger < p) {...}'.
  36. Note that n&M varies from 0 to M, *including* M, so that the
  37. array has to be of size M+1. Since M+1 is a power of two, n&M
  38. is a potentially quicker implementation of the equivalent n%(M+1).
  39. This implementation copyright (C) 1998 James Theiler, based on
  40. the example mt.c in the GSL, as implemented by Brian Gough.
  41. */
  42. #include "gsl__config.h"
  43. #include <stdlib.h>
  44. #include "gsl_rng.h"
  45. static inline unsigned long int gfsr4_get (void *vstate);
  46. static double gfsr4_get_double (void *vstate);
  47. static void gfsr4_set (void *state, unsigned long int s);
  48. /* Magic numbers */
  49. #define A 471
  50. #define B 1586
  51. #define C 6988
  52. #define D 9689
  53. #define M 16383 /* = 2^14-1 */
  54. /* #define M 0x0003fff */
  55. typedef struct
  56. {
  57. int nd;
  58. unsigned long ra[M+1];
  59. }
  60. gfsr4_state_t;
  61. static inline unsigned long
  62. gfsr4_get (void *vstate)
  63. {
  64. gfsr4_state_t *state = (gfsr4_state_t *) vstate;
  65. state->nd = ((state->nd)+1)&M;
  66. return state->ra[(state->nd)] =
  67. state->ra[((state->nd)+(M+1-A))&M]^
  68. state->ra[((state->nd)+(M+1-B))&M]^
  69. state->ra[((state->nd)+(M+1-C))&M]^
  70. state->ra[((state->nd)+(M+1-D))&M];
  71. }
  72. static double
  73. gfsr4_get_double (void * vstate)
  74. {
  75. return gfsr4_get (vstate) / 4294967296.0 ;
  76. }
  77. static void
  78. gfsr4_set (void *vstate, unsigned long int s)
  79. {
  80. gfsr4_state_t *state = (gfsr4_state_t *) vstate;
  81. int i, j;
  82. /* Masks for turning on the diagonal bit and turning off the
  83. leftmost bits */
  84. unsigned long int msb = 0x80000000UL;
  85. unsigned long int mask = 0xffffffffUL;
  86. if (s == 0)
  87. s = 4357; /* the default seed is 4357 */
  88. /* We use the congruence s_{n+1} = (69069*s_n) mod 2^32 to
  89. initialize the state. This works because ANSI-C unsigned long
  90. integer arithmetic is automatically modulo 2^32 (or a higher
  91. power of two), so we can safely ignore overflow. */
  92. #define LCG(n) ((69069 * n) & 0xffffffffUL)
  93. /* Brian Gough suggests this to avoid low-order bit correlations */
  94. for (i = 0; i <= M; i++)
  95. {
  96. unsigned long t = 0 ;
  97. unsigned long bit = msb ;
  98. for (j = 0; j < 32; j++)
  99. {
  100. s = LCG(s) ;
  101. if (s & msb)
  102. t |= bit ;
  103. bit >>= 1 ;
  104. }
  105. state->ra[i] = t ;
  106. }
  107. /* Perform the "orthogonalization" of the matrix */
  108. /* Based on the orthogonalization used in r250, as suggested initially
  109. * by Kirkpatrick and Stoll, and pointed out to me by Brian Gough
  110. */
  111. /* BJG: note that this orthogonalisation doesn't have any effect
  112. here because the the initial 6695 elements do not participate in
  113. the calculation. For practical purposes this orthogonalisation
  114. is somewhat irrelevant, because the probability of the original
  115. sequence being degenerate should be exponentially small. */
  116. for (i=0; i<32; ++i) {
  117. int k=7+i*3;
  118. state->ra[k] &= mask; /* Turn off bits left of the diagonal */
  119. state->ra[k] |= msb; /* Turn on the diagonal bit */
  120. mask >>= 1;
  121. msb >>= 1;
  122. }
  123. state->nd = i;
  124. }
  125. static const gsl_rng_type gfsr4_type =
  126. {"gfsr4", /* name */
  127. 0xffffffffUL, /* RAND_MAX */
  128. 0, /* RAND_MIN */
  129. sizeof (gfsr4_state_t),
  130. &gfsr4_set,
  131. &gfsr4_get,
  132. &gfsr4_get_double};
  133. const gsl_rng_type *gsl_rng_gfsr4 = &gfsr4_type;