gsl_rng__zuf.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* rng/zuf.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. /* It is crucial that m == n-273 mod 607 at all times;
  23. For speed of execution, however, this is never enforced.
  24. Instead is is set in the initializer: note 607-273=334
  25. Note also that the state.u[607] is not initialized */
  26. static inline unsigned long int zuf_get (void *vstate);
  27. static double zuf_get_double (void *vstate);
  28. static void zuf_set (void *state, unsigned long int s);
  29. static const unsigned long int zuf_randmax = 16777216; /* 2^24 */
  30. typedef struct
  31. {
  32. int n;
  33. unsigned long int u[607];
  34. }
  35. zuf_state_t;
  36. /* The zufall package was implemented with float's, which is to say 24
  37. bits of precision. Since I'm using long's instead, my RANDMAX
  38. reflects this. */
  39. static inline unsigned long int
  40. zuf_get (void *vstate)
  41. {
  42. zuf_state_t *state = (zuf_state_t *) vstate;
  43. const int n = state->n;
  44. const int m = (n - 273 + 607) % 607;
  45. unsigned long int t = state->u[n] + state->u[m];
  46. while (t > zuf_randmax)
  47. t -= zuf_randmax;
  48. state->u[n] = t;
  49. if (n == 606)
  50. {
  51. state->n = 0;
  52. }
  53. else
  54. {
  55. state->n = n + 1;
  56. }
  57. return t;
  58. }
  59. static double
  60. zuf_get_double (void *vstate)
  61. {
  62. return zuf_get (vstate) / 16777216.0 ;
  63. }
  64. static void
  65. zuf_set (void *vstate, unsigned long int s)
  66. {
  67. /* A very elaborate seeding procedure is provided with the
  68. zufall package; this is virtually a copy of that procedure */
  69. /* Initialized data */
  70. long int kl = 9373;
  71. long int ij = 1802;
  72. /* Local variables */
  73. long int i, j, k, l, m;
  74. double x, y;
  75. long int ii, jj;
  76. zuf_state_t *state = (zuf_state_t *) vstate;
  77. state->n = 0;
  78. /* generates initial seed buffer by linear congruential */
  79. /* method. Taken from Marsaglia, FSU report FSU-SCRI-87-50 */
  80. /* variable seed should be 0 < seed <31328 */
  81. if (s == 0)
  82. s = 1802; /* default seed is 1802 */
  83. ij = s;
  84. i = ij / 177 % 177 + 2;
  85. j = ij % 177 + 2;
  86. k = kl / 169 % 178 + 1;
  87. l = kl % 169;
  88. for (ii = 0; ii < 607; ++ii)
  89. {
  90. x = 0.0;
  91. y = 0.5;
  92. /* 24 bits?? */
  93. for (jj = 1; jj <= 24; ++jj)
  94. {
  95. m = i * j % 179 * k % 179;
  96. i = j;
  97. j = k;
  98. k = m;
  99. l = (l * 53 + 1) % 169;
  100. if (l * m % 64 >= 32)
  101. {
  102. x += y;
  103. }
  104. y *= 0.5;
  105. }
  106. state->u[ii] = (unsigned long int) (x * zuf_randmax);
  107. }
  108. }
  109. static const gsl_rng_type zuf_type =
  110. {"zuf", /* name */
  111. 0x00ffffffUL, /* RAND_MAX */
  112. 0, /* RAND_MIN */
  113. sizeof (zuf_state_t),
  114. &zuf_set,
  115. &zuf_get,
  116. &zuf_get_double};
  117. const gsl_rng_type *gsl_rng_zuf = &zuf_type;