gsl_rng__ran2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* rng/ran2.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 an implementation of the algorithm used in Numerical
  23. Recipe's ran2 generator. It is a L'Ecuyer combined recursive
  24. generator with a 32-element shuffle-box.
  25. As far as I can tell, in general the effects of adding a shuffle
  26. box cannot be proven theoretically, so the period of this generator
  27. is unknown.
  28. The period of the underlying combined generator is O(2^60). */
  29. static inline unsigned long int ran2_get (void *vstate);
  30. static double ran2_get_double (void *vstate);
  31. static void ran2_set (void *state, unsigned long int s);
  32. static const long int m1 = 2147483563, a1 = 40014, q1 = 53668, r1 = 12211;
  33. static const long int m2 = 2147483399, a2 = 40692, q2 = 52774, r2 = 3791;
  34. #define N_SHUFFLE 32
  35. #define N_DIV (1 + 2147483562/N_SHUFFLE)
  36. typedef struct
  37. {
  38. unsigned long int x;
  39. unsigned long int y;
  40. unsigned long int n;
  41. unsigned long int shuffle[N_SHUFFLE];
  42. }
  43. ran2_state_t;
  44. static inline unsigned long int
  45. ran2_get (void *vstate)
  46. {
  47. ran2_state_t *state = (ran2_state_t *) vstate;
  48. const unsigned long int x = state->x;
  49. const unsigned long int y = state->y;
  50. long int h1 = x / q1;
  51. long int t1 = a1 * (x - h1 * q1) - h1 * r1;
  52. long int h2 = y / q2;
  53. long int t2 = a2 * (y - h2 * q2) - h2 * r2;
  54. if (t1 < 0)
  55. t1 += m1;
  56. if (t2 < 0)
  57. t2 += m2;
  58. state->x = t1;
  59. state->y = t2;
  60. {
  61. unsigned long int j = state->n / N_DIV;
  62. long int delta = state->shuffle[j] - t2;
  63. if (delta < 1)
  64. delta += m1 - 1;
  65. state->n = delta;
  66. state->shuffle[j] = t1;
  67. }
  68. return state->n;
  69. }
  70. static double
  71. ran2_get_double (void *vstate)
  72. {
  73. float x_max = 1 - 1.2e-7f ; /* Numerical Recipes version of 1-FLT_EPS */
  74. float x = ran2_get (vstate) / 2147483563.0f ;
  75. if (x > x_max)
  76. return x_max ;
  77. return x ;
  78. }
  79. static void
  80. ran2_set (void *vstate, unsigned long int s)
  81. {
  82. ran2_state_t *state = (ran2_state_t *) vstate;
  83. int i;
  84. if (s == 0)
  85. s = 1; /* default seed is 1 */
  86. state->y = s;
  87. for (i = 0; i < 8; i++)
  88. {
  89. long int h = s / q1;
  90. long int t = a1 * (s - h * q1) - h * r1;
  91. if (t < 0)
  92. t += m1;
  93. s = t;
  94. }
  95. for (i = N_SHUFFLE - 1; i >= 0; i--)
  96. {
  97. long int h = s / q1;
  98. long int t = a1 * (s - h * q1) - h * r1;
  99. if (t < 0)
  100. t += m1;
  101. s = t;
  102. state->shuffle[i] = s;
  103. }
  104. state->x = s;
  105. state->n = s;
  106. return;
  107. }
  108. static const gsl_rng_type ran2_type =
  109. {"ran2", /* name */
  110. 2147483562, /* RAND_MAX */
  111. 1, /* RAND_MIN */
  112. sizeof (ran2_state_t),
  113. &ran2_set,
  114. &ran2_get,
  115. &ran2_get_double};
  116. const gsl_rng_type *gsl_rng_ran2 = &ran2_type;