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 a shift-register random number generator. The sequence is
26 x_n = x_{n-103} ^ x_{n-250} ("^" means XOR)
28 defined on 32-bit words.
30 BJG: Note that this implementation actually uses the sequence, x_n
31 = x_{n-147} ^ x_{n-250} which generates the outputs in
32 time-reversed order but is otherwise completely equivalent.
34 The first 250 elements x_1 .. x_250 are first initialized as x_n =
35 s_n, where s_n = (69069*s_{n-1}) mod 2^32 and s_0=s is the
36 user-supplied seed. To ensure that the sequence does not lie on a
37 subspace we force 32 of the entries to be linearly independent. We
38 take the 32 elements x[3], x[10], x[17], x[24], ..., 213 and apply
39 the following operations,
41 x[3] &= 11111111111111111111111111111111
42 x[3] |= 10000000000000000000000000000000
43 x[10] &= 01111111111111111111111111111111
44 x[10] |= 01000000000000000000000000000000
45 x[17] &= 00111111111111111111111111111111
46 x[17] |= 00100000000000000000000000000000
48 x[206] &= 00000000000000000000000000000111
49 x[206] |= 00000000000000000000000000000100
50 x[213] &= 00000000000000000000000000000011
51 x[213] |= 00000000000000000000000000000010
52 x[220] &= 00000000000000000000000000000001
53 x[220] |= 00000000000000000000000000000001
55 i.e. if we consider the bits of the 32 elements as forming a 32x32
56 array then we are setting the diagonal bits of the array to one and
57 masking the lower triangle below the diagonal to zero.
59 With this initialization procedure the theoretical value of
60 x_{10001} is 1100653588 for s = 1 (Actually I got this by running
61 the original code). The subscript 10001 means (1) seed the
62 generator with s = 1 and then do 10000 actual iterations.
64 The period of this generator is about 2^250.
66 The algorithm works for any number of bits. It is implemented here
69 From: S. Kirkpatrick and E. Stoll, "A very fast shift-register
70 sequence random number generator", Journal of Computational Physics,
71 40, 517-526 (1981). */
73 static inline unsigned long int r250_get (void *vstate);
74 static double r250_get_double (void *vstate);
75 static void r250_set (void *state, unsigned long int s);
84 static inline unsigned long int
85 r250_get (void *vstate)
87 r250_state_t *state = (r250_state_t *) vstate;
102 k = state->x[i] ^ state->x[j];
118 r250_get_double (void *vstate)
120 return r250_get (vstate) / 4294967296.0 ;
124 r250_set (void *vstate, unsigned long int s)
126 r250_state_t *state = (r250_state_t *) vstate;
131 s = 1; /* default seed is 1 */
135 #define LCG(n) ((69069 * n) & 0xffffffffUL)
137 for (i = 0; i < 250; i++) /* Fill the buffer */
144 /* Masks for turning on the diagonal bit and turning off the
147 unsigned long int msb = 0x80000000UL;
148 unsigned long int mask = 0xffffffffUL;
150 for (i = 0; i < 32; i++)
152 int k = 7 * i + 3; /* Select a word to operate on */
153 state->x[k] &= mask; /* Turn off bits left of the diagonal */
154 state->x[k] |= msb; /* Turn on the diagonal bit */
163 static const gsl_rng_type r250_type =
165 0xffffffffUL, /* RAND_MAX */
167 sizeof (r250_state_t),
172 const gsl_rng_type *gsl_rng_r250 = &r250_type;