gsl_rng__mrg.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* rng/mrg.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 fifth-order multiple recursive generator. The sequence is,
  23. x_n = (a_1 x_{n-1} + a_5 x_{n-5}) mod m
  24. with a_1 = 107374182, a_2 = a_3 = a_4 = 0, a_5 = 104480 and m = 2^31-1.
  25. We initialize the generator with x_n = s_n MOD m for n = 1..5,
  26. where s_n = (69069 * s_{n-1}) mod 2^32, and s_0 = s is the
  27. user-supplied seed.
  28. NOTE: According to the paper the seeds must lie in the range [0,
  29. 2^31 - 2] with at least one non-zero value -- our seeding procedure
  30. satisfies these constraints.
  31. We then use 6 iterations of the generator to "warm up" the internal
  32. state.
  33. With this initialization procedure the theoretical value of
  34. z_{10006} is 2064828650 for s = 1. The subscript 10006 means (1)
  35. seed the generator with s = 1, (2) do the 6 warm-up iterations
  36. that are part of the seeding process, (3) then do 10000 actual
  37. iterations.
  38. The period of this generator is about 2^155.
  39. From: P. L'Ecuyer, F. Blouin, and R. Coutre, "A search for good
  40. multiple recursive random number generators", ACM Transactions on
  41. Modeling and Computer Simulation 3, 87-98 (1993). */
  42. static inline unsigned long int mrg_get (void *vstate);
  43. static double mrg_get_double (void *vstate);
  44. static void mrg_set (void *state, unsigned long int s);
  45. static const long int m = 2147483647;
  46. static const long int a1 = 107374182, q1 = 20, r1 = 7;
  47. static const long int a5 = 104480, q5 = 20554, r5 = 1727;
  48. typedef struct
  49. {
  50. long int x1, x2, x3, x4, x5;
  51. }
  52. mrg_state_t;
  53. static inline unsigned long int
  54. mrg_get (void *vstate)
  55. {
  56. mrg_state_t *state = (mrg_state_t *) vstate;
  57. long int p1, h1, p5, h5;
  58. h5 = state->x5 / q5;
  59. p5 = a5 * (state->x5 - h5 * q5) - h5 * r5;
  60. if (p5 > 0)
  61. p5 -= m;
  62. h1 = state->x1 / q1;
  63. p1 = a1 * (state->x1 - h1 * q1) - h1 * r1;
  64. if (p1 < 0)
  65. p1 += m;
  66. state->x5 = state->x4;
  67. state->x4 = state->x3;
  68. state->x3 = state->x2;
  69. state->x2 = state->x1;
  70. state->x1 = p1 + p5;
  71. if (state->x1 < 0)
  72. state->x1 += m;
  73. return state->x1;
  74. }
  75. static double
  76. mrg_get_double (void *vstate)
  77. {
  78. return mrg_get (vstate) / 2147483647.0 ;
  79. }
  80. static void
  81. mrg_set (void *vstate, unsigned long int s)
  82. {
  83. /* An entirely adhoc way of seeding! This does **not** come from
  84. L'Ecuyer et al */
  85. mrg_state_t *state = (mrg_state_t *) vstate;
  86. if (s == 0)
  87. s = 1; /* default seed is 1 */
  88. #define LCG(n) ((69069 * n) & 0xffffffffUL)
  89. s = LCG (s);
  90. state->x1 = s % m;
  91. s = LCG (s);
  92. state->x2 = s % m;
  93. s = LCG (s);
  94. state->x3 = s % m;
  95. s = LCG (s);
  96. state->x4 = s % m;
  97. s = LCG (s);
  98. state->x5 = s % m;
  99. /* "warm it up" with at least 5 calls to go through
  100. all the x values */
  101. mrg_get (state);
  102. mrg_get (state);
  103. mrg_get (state);
  104. mrg_get (state);
  105. mrg_get (state);
  106. mrg_get (state);
  107. return;
  108. }
  109. static const gsl_rng_type mrg_type =
  110. {"mrg", /* name */
  111. 2147483646, /* RAND_MAX */
  112. 0, /* RAND_MIN */
  113. sizeof (mrg_state_t),
  114. &mrg_set,
  115. &mrg_get,
  116. &mrg_get_double};
  117. const gsl_rng_type *gsl_rng_mrg = &mrg_type;