gsl_rng__r250.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* rng/r250.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 shift-register random number generator. The sequence is
  23. x_n = x_{n-103} ^ x_{n-250} ("^" means XOR)
  24. defined on 32-bit words.
  25. BJG: Note that this implementation actually uses the sequence, x_n
  26. = x_{n-147} ^ x_{n-250} which generates the outputs in
  27. time-reversed order but is otherwise completely equivalent.
  28. The first 250 elements x_1 .. x_250 are first initialized as x_n =
  29. s_n, where s_n = (69069*s_{n-1}) mod 2^32 and s_0=s is the
  30. user-supplied seed. To ensure that the sequence does not lie on a
  31. subspace we force 32 of the entries to be linearly independent. We
  32. take the 32 elements x[3], x[10], x[17], x[24], ..., 213 and apply
  33. the following operations,
  34. x[3] &= 11111111111111111111111111111111
  35. x[3] |= 10000000000000000000000000000000
  36. x[10] &= 01111111111111111111111111111111
  37. x[10] |= 01000000000000000000000000000000
  38. x[17] &= 00111111111111111111111111111111
  39. x[17] |= 00100000000000000000000000000000
  40. .... ...
  41. x[206] &= 00000000000000000000000000000111
  42. x[206] |= 00000000000000000000000000000100
  43. x[213] &= 00000000000000000000000000000011
  44. x[213] |= 00000000000000000000000000000010
  45. x[220] &= 00000000000000000000000000000001
  46. x[220] |= 00000000000000000000000000000001
  47. i.e. if we consider the bits of the 32 elements as forming a 32x32
  48. array then we are setting the diagonal bits of the array to one and
  49. masking the lower triangle below the diagonal to zero.
  50. With this initialization procedure the theoretical value of
  51. x_{10001} is 1100653588 for s = 1 (Actually I got this by running
  52. the original code). The subscript 10001 means (1) seed the
  53. generator with s = 1 and then do 10000 actual iterations.
  54. The period of this generator is about 2^250.
  55. The algorithm works for any number of bits. It is implemented here
  56. for 32 bits.
  57. From: S. Kirkpatrick and E. Stoll, "A very fast shift-register
  58. sequence random number generator", Journal of Computational Physics,
  59. 40, 517-526 (1981). */
  60. static inline unsigned long int r250_get (void *vstate);
  61. static double r250_get_double (void *vstate);
  62. static void r250_set (void *state, unsigned long int s);
  63. typedef struct
  64. {
  65. int i;
  66. unsigned long x[250];
  67. }
  68. r250_state_t;
  69. static inline unsigned long int
  70. r250_get (void *vstate)
  71. {
  72. r250_state_t *state = (r250_state_t *) vstate;
  73. unsigned long int k;
  74. int j;
  75. int i = state->i;
  76. if (i >= 147)
  77. {
  78. j = i - 147;
  79. }
  80. else
  81. {
  82. j = i + 103;
  83. }
  84. k = state->x[i] ^ state->x[j];
  85. state->x[i] = k;
  86. if (i >= 249)
  87. {
  88. state->i = 0;
  89. }
  90. else
  91. {
  92. state->i = i + 1;
  93. }
  94. return k;
  95. }
  96. static double
  97. r250_get_double (void *vstate)
  98. {
  99. return r250_get (vstate) / 4294967296.0 ;
  100. }
  101. static void
  102. r250_set (void *vstate, unsigned long int s)
  103. {
  104. r250_state_t *state = (r250_state_t *) vstate;
  105. int i;
  106. if (s == 0)
  107. s = 1; /* default seed is 1 */
  108. state->i = 0;
  109. #define LCG(n) ((69069 * n) & 0xffffffffUL)
  110. for (i = 0; i < 250; i++) /* Fill the buffer */
  111. {
  112. s = LCG (s);
  113. state->x[i] = s;
  114. }
  115. {
  116. /* Masks for turning on the diagonal bit and turning off the
  117. leftmost bits */
  118. unsigned long int msb = 0x80000000UL;
  119. unsigned long int mask = 0xffffffffUL;
  120. for (i = 0; i < 32; i++)
  121. {
  122. int k = 7 * i + 3; /* Select a word to operate on */
  123. state->x[k] &= mask; /* Turn off bits left of the diagonal */
  124. state->x[k] |= msb; /* Turn on the diagonal bit */
  125. mask >>= 1;
  126. msb >>= 1;
  127. }
  128. }
  129. return;
  130. }
  131. static const gsl_rng_type r250_type =
  132. {"r250", /* name */
  133. 0xffffffffUL, /* RAND_MAX */
  134. 0, /* RAND_MIN */
  135. sizeof (r250_state_t),
  136. &r250_set,
  137. &r250_get,
  138. &r250_get_double};
  139. const gsl_rng_type *gsl_rng_r250 = &r250_type;