3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
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.
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.
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.
22 #include <gsl/gsl_rng.h>
24 /* This is the RANMAR lagged fibonacci generator of Marsaglia, Zaman
25 and Tsang. The sequence is a series of 24-bit integers, x_n,
27 x_n = (y_n - c_n + 2^24) mod 2^24
31 y_n = (y_{n-97) - y_{n-33} + 2^24) mod 2^24
32 c_n = (c_{n-1} - 7654321 + 2^24 - 3) mod (2^24 - 3)
34 The period of this generator is 2^144.
36 The generator provides about 900 million different subsequences
37 each of length O(10^30). Thus each seed up to 900,000,000 gives an
40 Although it was good in its day this generator now has known
41 statistical defects and has been superseded by RANLUX.
43 From: F. James, "A Review of Pseudorandom number generators",
44 Computer Physics Communications 60, 329 (1990).
46 G. Marsaglia, A. Zaman and W.W. Tsang, Stat. Prob. Lett. 9, 35 (1990) */
48 static inline unsigned long int ranmar_get (void *vstate);
49 static double ranmar_get_double (void *vstate);
50 static void ranmar_set (void *state, unsigned long int s);
52 static const unsigned long int two24 = 16777216; /* 2^24 */
59 unsigned long int u[97];
63 static inline unsigned long int
64 ranmar_get (void *vstate)
66 ranmar_state_t *state = (ranmar_state_t *) vstate;
68 unsigned int i = state->i;
69 unsigned int j = state->j;
70 long int carry = state->carry;
72 long int delta = state->u[i] - state->u[j];
106 state->carry = carry ;
117 ranmar_get_double (void *vstate)
119 return ranmar_get (vstate) / 16777216.0 ;
123 ranmar_set (void *vstate, unsigned long int s)
125 ranmar_state_t *state = (ranmar_state_t *) vstate;
127 unsigned long int ij = s / 30082 ;
128 unsigned long int kl = s % 30082 ;
130 int i = (ij / 177) % 177 + 2 ;
131 int j = (ij % 177) + 2 ;
132 int k = (kl / 169) % 178 + 1 ;
137 for (a = 0; a < 97; a++)
139 unsigned long int sum = 0 ;
140 unsigned long int t = two24 ;
142 for (b = 0; b < 24; b++)
144 unsigned long int m = (((i * j) % 179) * k) % 179 ;
148 l = (53 * l + 1) % 169 ;
151 if ((l * m) % 64 >= 32)
160 state->carry = 362436 ;
164 static const gsl_rng_type ranmar_type =
165 {"ranmar", /* name */
166 0x00ffffffUL, /* RAND_MAX */
168 sizeof (ranmar_state_t),
173 const gsl_rng_type *gsl_rng_ranmar = &ranmar_type;