gsl_rng__ranmar.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* rng/ranmar.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 the RANMAR lagged fibonacci generator of Marsaglia, Zaman
  23. and Tsang. The sequence is a series of 24-bit integers, x_n,
  24. x_n = (y_n - c_n + 2^24) mod 2^24
  25. where,
  26. y_n = (y_{n-97) - y_{n-33} + 2^24) mod 2^24
  27. c_n = (c_{n-1} - 7654321 + 2^24 - 3) mod (2^24 - 3)
  28. The period of this generator is 2^144.
  29. The generator provides about 900 million different subsequences
  30. each of length O(10^30). Thus each seed up to 900,000,000 gives an
  31. independent sequence.
  32. Although it was good in its day this generator now has known
  33. statistical defects and has been superseded by RANLUX.
  34. From: F. James, "A Review of Pseudorandom number generators",
  35. Computer Physics Communications 60, 329 (1990).
  36. G. Marsaglia, A. Zaman and W.W. Tsang, Stat. Prob. Lett. 9, 35 (1990) */
  37. static inline unsigned long int ranmar_get (void *vstate);
  38. static double ranmar_get_double (void *vstate);
  39. static void ranmar_set (void *state, unsigned long int s);
  40. static const unsigned long int two24 = 16777216; /* 2^24 */
  41. typedef struct
  42. {
  43. unsigned int i;
  44. unsigned int j;
  45. long int carry;
  46. unsigned long int u[97];
  47. }
  48. ranmar_state_t;
  49. static inline unsigned long int
  50. ranmar_get (void *vstate)
  51. {
  52. ranmar_state_t *state = (ranmar_state_t *) vstate;
  53. unsigned int i = state->i;
  54. unsigned int j = state->j;
  55. long int carry = state->carry;
  56. long int delta = state->u[i] - state->u[j];
  57. if (delta < 0)
  58. delta += two24 ;
  59. state->u[i] = delta;
  60. if (i == 0)
  61. {
  62. i = 96;
  63. }
  64. else
  65. {
  66. i--;
  67. }
  68. state->i = i;
  69. if (j == 0)
  70. {
  71. j = 96;
  72. }
  73. else
  74. {
  75. j--;
  76. }
  77. state->j = j;
  78. carry += - 7654321 ;
  79. if (carry < 0)
  80. carry += two24 - 3;
  81. state->carry = carry ;
  82. delta += - carry ;
  83. if (delta < 0)
  84. delta += two24 ;
  85. return delta;
  86. }
  87. static double
  88. ranmar_get_double (void *vstate)
  89. {
  90. return ranmar_get (vstate) / 16777216.0 ;
  91. }
  92. static void
  93. ranmar_set (void *vstate, unsigned long int s)
  94. {
  95. ranmar_state_t *state = (ranmar_state_t *) vstate;
  96. unsigned long int ij = s / 30082 ;
  97. unsigned long int kl = s % 30082 ;
  98. int i = (ij / 177) % 177 + 2 ;
  99. int j = (ij % 177) + 2 ;
  100. int k = (kl / 169) % 178 + 1 ;
  101. int l = (kl % 169) ;
  102. int a, b;
  103. for (a = 0; a < 97; a++)
  104. {
  105. unsigned long int sum = 0 ;
  106. unsigned long int t = two24 ;
  107. for (b = 0; b < 24; b++)
  108. {
  109. unsigned long int m = (((i * j) % 179) * k) % 179 ;
  110. i = j ;
  111. j = k ;
  112. k = m ;
  113. l = (53 * l + 1) % 169 ;
  114. t >>= 1 ;
  115. if ((l * m) % 64 >= 32)
  116. sum += t ;
  117. }
  118. state->u[a] = sum ;
  119. }
  120. state->i = 96;
  121. state->j = 32;
  122. state->carry = 362436 ;
  123. }
  124. static const gsl_rng_type ranmar_type =
  125. {"ranmar", /* name */
  126. 0x00ffffffUL, /* RAND_MAX */
  127. 0, /* RAND_MIN */
  128. sizeof (ranmar_state_t),
  129. &ranmar_set,
  130. &ranmar_get,
  131. &ranmar_get_double};
  132. const gsl_rng_type *gsl_rng_ranmar = &ranmar_type;