gsl_rng__knuthran.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* rng/knuthran.c
  2. *
  3. * Copyright (C) 2001, 2007 Brian Gough, Carlo Perassi
  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. /*
  20. * This generator is taken from
  21. *
  22. * Donald E. Knuth
  23. * The Art of Computer Programming
  24. * Volume 2
  25. * Third Edition
  26. * Addison-Wesley
  27. * Section 3.6
  28. *
  29. * The comments are taken from the book
  30. * Our comments are signed
  31. */
  32. #include "gsl__config.h"
  33. #include <stdlib.h>
  34. #include "gsl_rng.h"
  35. #define BUFLEN 2009 /* [Brian]: length of the buffer aa[] */
  36. #define KK 100 /* the long lag */
  37. #define LL 37 /* the short lag */
  38. #define MM (1L << 30) /* the modulus */
  39. #define TT 70 /* guaranteed separation between streams */
  40. #define evenize(x) ((x) & (MM - 2)) /* make x even */
  41. #define is_odd(x) ((x) & 1) /* the units bit of x */
  42. #define mod_diff(x, y) (((x) - (y)) & (MM - 1)) /* (x - y) mod MM */
  43. static inline void ran_array (unsigned long int aa[], unsigned int n,
  44. unsigned long int ran_x[]);
  45. static inline unsigned long int ran_get (void *vstate);
  46. static double ran_get_double (void *vstate);
  47. static void ran_set (void *state, unsigned long int s);
  48. typedef struct
  49. {
  50. unsigned int i;
  51. unsigned long int aa[BUFLEN]; /* [Carlo]: I can't pass n to ran_array like
  52. Knuth does */
  53. unsigned long int ran_x[KK]; /* the generator state */
  54. }
  55. ran_state_t;
  56. static inline void
  57. ran_array (unsigned long int aa[], unsigned int n, unsigned long int ran_x[])
  58. {
  59. unsigned int i;
  60. unsigned int j;
  61. for (j = 0; j < KK; j++)
  62. aa[j] = ran_x[j];
  63. for (; j < n; j++)
  64. aa[j] = mod_diff (aa[j - KK], aa[j - LL]);
  65. for (i = 0; i < LL; i++, j++)
  66. ran_x[i] = mod_diff (aa[j - KK], aa[j - LL]);
  67. for (; i < KK; i++, j++)
  68. ran_x[i] = mod_diff (aa[j - KK], ran_x[i - LL]);
  69. }
  70. static inline unsigned long int
  71. ran_get (void *vstate)
  72. {
  73. ran_state_t *state = (ran_state_t *) vstate;
  74. unsigned int i = state->i;
  75. if (i == 0)
  76. {
  77. /* fill buffer with new random numbers */
  78. ran_array (state->aa, BUFLEN, state->ran_x);
  79. }
  80. state->i = (i + 1) % BUFLEN;
  81. return state->aa[i];
  82. }
  83. static double
  84. ran_get_double (void *vstate)
  85. {
  86. ran_state_t *state = (ran_state_t *) vstate;
  87. return ran_get (state) / 1073741824.0; /* [Carlo]: RAND_MAX + 1 */
  88. }
  89. static void
  90. ran_set (void *vstate, unsigned long int s)
  91. {
  92. ran_state_t *state = (ran_state_t *) vstate;
  93. long x[KK + KK - 1]; /* the preparation buffer */
  94. register int j;
  95. register int t;
  96. register long ss = evenize (s + 2);
  97. for (j = 0; j < KK; j++)
  98. {
  99. x[j] = ss; /* bootstrap the buffer */
  100. ss <<= 1;
  101. if (ss >= MM) /* cyclic shift 29 bits */
  102. ss -= MM - 2;
  103. }
  104. for (; j < KK + KK - 1; j++)
  105. x[j] = 0;
  106. x[1]++; /* make x[1] (and only x[1]) odd */
  107. ss = s & (MM - 1);
  108. t = TT - 1;
  109. while (t)
  110. {
  111. for (j = KK - 1; j > 0; j--) /* square */
  112. x[j + j] = x[j];
  113. for (j = KK + KK - 2; j > KK - LL; j -= 2)
  114. x[KK + KK - 1 - j] = evenize (x[j]);
  115. for (j = KK + KK - 2; j >= KK; j--)
  116. if (is_odd (x[j]))
  117. {
  118. x[j - (KK - LL)] = mod_diff (x[j - (KK - LL)], x[j]);
  119. x[j - KK] = mod_diff (x[j - KK], x[j]);
  120. }
  121. if (is_odd (ss))
  122. { /* multiply by "z" */
  123. for (j = KK; j > 0; j--)
  124. x[j] = x[j - 1];
  125. x[0] = x[KK]; /* shift the buffer cyclically */
  126. if (is_odd (x[KK]))
  127. x[LL] = mod_diff (x[LL], x[KK]);
  128. }
  129. if (ss)
  130. ss >>= 1;
  131. else
  132. t--;
  133. }
  134. state->i = 0;
  135. for (j = 0; j < LL; j++)
  136. state->ran_x[j + KK - LL] = x[j];
  137. for (; j < KK; j++)
  138. state->ran_x[j - LL] = x[j];
  139. return;
  140. }
  141. static const gsl_rng_type ran_type = {
  142. "knuthran", /* name */
  143. 0x3fffffffUL, /* RAND_MAX *//* [Carlo]: (2 ^ 30) - 1 */
  144. 0, /* RAND_MIN */
  145. sizeof (ran_state_t),
  146. &ran_set,
  147. &ran_get,
  148. &ran_get_double
  149. };
  150. const gsl_rng_type *gsl_rng_knuthran = &ran_type;