gsl_rng__cmrg.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* rng/cmrg.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 combined multiple recursive generator. The sequence is,
  23. z_n = (x_n - y_n) mod m1
  24. where the two underlying generators x and y are,
  25. x_n = (a_{1} x_{n-1} + a_{2} x_{n-2} + a_{3} x_{n-3}) mod m1
  26. y_n = (b_{1} y_{n-1} + b_{2} y_{n-2} + b_{3} y_{n-3}) mod m2
  27. with coefficients a11 ... a23,
  28. a_{1} = 0, a_{2} = 63308, a_{3} = -183326
  29. b_{1} = 86098, b_{2} = 0, b_{3} = -539608
  30. and moduli m1, m2,
  31. m1 = 2^31 - 1 = 2147483647
  32. m2 = 2^31 - 2000169 = 2145483479
  33. We initialize the generator with
  34. x_1 = s_1 MOD m1, x_2 = s_2 MOD m1, x_3 = s_3 MOD m1
  35. y_1 = s_4 MOD m2, y_2 = s_5 MOD m2, y_3 = s_6 MOD m2
  36. where s_n = (69069 * s_{n-1}) mod 2^32 and s_0 = s is the
  37. user-supplied seed.
  38. NOTE: According to the paper the initial values for x_n must lie in
  39. the range 0 <= x_n <= (m1 - 1) and the initial values for y_n must
  40. lie in the range 0 <= y_n <= (m2 - 1), with at least one non-zero
  41. value -- our seeding procedure satisfies these constraints.
  42. We then use 7 iterations of the generator to "warm up" the internal
  43. state.
  44. The theoretical value of z_{10008} is 719452880. The subscript 10008
  45. means (1) seed the generator with s=1, (2) do the seven warm-up
  46. iterations that are part of the seeding process, (3) then do 10000
  47. actual iterations.
  48. The period of this generator is about 2^205.
  49. From: P. L'Ecuyer, "Combined Multiple Recursive Random Number
  50. Generators," Operations Research, 44, 5 (1996), 816--822.
  51. This is available on the net from L'Ecuyer's home page,
  52. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/combmrg.ps
  53. ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/combmrg.ps */
  54. static inline unsigned long int cmrg_get (void *vstate);
  55. static double cmrg_get_double (void *vstate);
  56. static void cmrg_set (void *state, unsigned long int s);
  57. static const long int m1 = 2147483647, m2 = 2145483479;
  58. static const long int a2 = 63308, qa2 = 33921, ra2 = 12979;
  59. static const long int a3 = -183326, qa3 = 11714, ra3 = 2883;
  60. static const long int b1 = 86098, qb1 = 24919, rb1 = 7417;
  61. static const long int b3 = -539608, qb3 = 3976, rb3 = 2071;
  62. typedef struct
  63. {
  64. long int x1, x2, x3; /* first component */
  65. long int y1, y2, y3; /* second component */
  66. }
  67. cmrg_state_t;
  68. static inline unsigned long int
  69. cmrg_get (void *vstate)
  70. {
  71. cmrg_state_t *state = (cmrg_state_t *) vstate;
  72. /* Component 1 */
  73. {
  74. long int h3 = state->x3 / qa3;
  75. long int p3 = -a3 * (state->x3 - h3 * qa3) - h3 * ra3;
  76. long int h2 = state->x2 / qa2;
  77. long int p2 = a2 * (state->x2 - h2 * qa2) - h2 * ra2;
  78. if (p3 < 0)
  79. p3 += m1;
  80. if (p2 < 0)
  81. p2 += m1;
  82. state->x3 = state->x2;
  83. state->x2 = state->x1;
  84. state->x1 = p2 - p3;
  85. if (state->x1 < 0)
  86. state->x1 += m1;
  87. }
  88. /* Component 2 */
  89. {
  90. long int h3 = state->y3 / qb3;
  91. long int p3 = -b3 * (state->y3 - h3 * qb3) - h3 * rb3;
  92. long int h1 = state->y1 / qb1;
  93. long int p1 = b1 * (state->y1 - h1 * qb1) - h1 * rb1;
  94. if (p3 < 0)
  95. p3 += m2;
  96. if (p1 < 0)
  97. p1 += m2;
  98. state->y3 = state->y2;
  99. state->y2 = state->y1;
  100. state->y1 = p1 - p3;
  101. if (state->y1 < 0)
  102. state->y1 += m2;
  103. }
  104. if (state->x1 < state->y1)
  105. return (state->x1 - state->y1 + m1);
  106. else
  107. return (state->x1 - state->y1);
  108. }
  109. static double
  110. cmrg_get_double (void *vstate)
  111. {
  112. return cmrg_get (vstate) / 2147483647.0 ;
  113. }
  114. static void
  115. cmrg_set (void *vstate, unsigned long int s)
  116. {
  117. /* An entirely adhoc way of seeding! This does **not** come from
  118. L'Ecuyer et al */
  119. cmrg_state_t *state = (cmrg_state_t *) vstate;
  120. if (s == 0)
  121. s = 1; /* default seed is 1 */
  122. #define LCG(n) ((69069 * n) & 0xffffffffUL)
  123. s = LCG (s);
  124. state->x1 = s % m1;
  125. s = LCG (s);
  126. state->x2 = s % m1;
  127. s = LCG (s);
  128. state->x3 = s % m1;
  129. s = LCG (s);
  130. state->y1 = s % m2;
  131. s = LCG (s);
  132. state->y2 = s % m2;
  133. s = LCG (s);
  134. state->y3 = s % m2;
  135. /* "warm it up" */
  136. cmrg_get (state);
  137. cmrg_get (state);
  138. cmrg_get (state);
  139. cmrg_get (state);
  140. cmrg_get (state);
  141. cmrg_get (state);
  142. cmrg_get (state);
  143. }
  144. static const gsl_rng_type cmrg_type =
  145. {"cmrg", /* name */
  146. 2147483646, /* RAND_MAX */
  147. 0, /* RAND_MIN */
  148. sizeof (cmrg_state_t),
  149. &cmrg_set,
  150. &cmrg_get,
  151. &cmrg_get_double};
  152. const gsl_rng_type *gsl_rng_cmrg = &cmrg_type;