1 /* This program is free software; you can redistribute it and/or
2 modify it under the terms of the GNU General Public License as
3 published by the Free Software Foundation; either version 3 of the
4 License, or (at your option) any later version.
6 This program is distributed in the hope that it will be useful, but
7 WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 General Public License for more details. You should have received
10 a copy of the GNU General Public License along with this program;
11 if not, write to the Free Foundation, Inc., 59 Temple Place, Suite
12 330, Boston, MA 02111-1307 USA
14 From Robert M. Ziff, "Four-tap shift-register-sequence
15 random-number generators," Computers in Physics 12(4), Jul/Aug
16 1998, pp 385-392. A generalized feedback shift-register (GFSR)
17 is basically an xor-sum of particular past lagged values. A
18 four-tap register looks like:
19 ra[nd] = ra[nd-A] ^ ra[nd-B] ^ ra[nd-C] ^ ra[nd-D]
21 Ziff notes that "it is now widely known" that two-tap registers
22 have serious flaws, the most obvious one being the three-point
23 correlation that comes from the defn of the generator. Nice
24 mathematical properties can be derived for GFSR's, and numerics
25 bears out the claim that 4-tap GFSR's with appropriately chosen
26 offsets are as random as can be measured, using the author's test.
28 This implementation uses the values suggested the the author's
29 example on p392, but altered to fit the GSL framework. The "state"
30 is 2^14 longs, or 64Kbytes; 2^14 is the smallest power of two that
31 is larger than D, the largest offset. We really only need a state
32 with the last D values, but by going to a power of two, we can do a
33 masking operation instead of a modulo, and this is presumably
34 faster, though I haven't actually tried it. The article actually
35 suggested a short/fast hack:
37 #define RandomInteger (++nd, ra[nd&M]=ra[(nd-A)&M]\
38 ^ra[(nd-B)&M]^ra[(nd-C)&M]^ra[(nd-D)&M])
40 so that (as long as you've defined nd,ra[M+1]), then you ca do things
41 like: 'if (RandomInteger < p) {...}'.
43 Note that n&M varies from 0 to M, *including* M, so that the
44 array has to be of size M+1. Since M+1 is a power of two, n&M
45 is a potentially quicker implementation of the equivalent n%(M+1).
47 This implementation copyright (C) 1998 James Theiler, based on
48 the example mt.c in the GSL, as implemented by Brian Gough.
53 #include <gsl/gsl_rng.h>
55 static inline unsigned long int gfsr4_get (void *vstate);
56 static double gfsr4_get_double (void *vstate);
57 static void gfsr4_set (void *state, unsigned long int s);
64 #define M 16383 /* = 2^14-1 */
65 /* #define M 0x0003fff */
70 unsigned long ra[M+1];
74 static inline unsigned long
75 gfsr4_get (void *vstate)
77 gfsr4_state_t *state = (gfsr4_state_t *) vstate;
79 state->nd = ((state->nd)+1)&M;
80 return state->ra[(state->nd)] =
81 state->ra[((state->nd)+(M+1-A))&M]^
82 state->ra[((state->nd)+(M+1-B))&M]^
83 state->ra[((state->nd)+(M+1-C))&M]^
84 state->ra[((state->nd)+(M+1-D))&M];
89 gfsr4_get_double (void * vstate)
91 return gfsr4_get (vstate) / 4294967296.0 ;
95 gfsr4_set (void *vstate, unsigned long int s)
97 gfsr4_state_t *state = (gfsr4_state_t *) vstate;
99 /* Masks for turning on the diagonal bit and turning off the
101 unsigned long int msb = 0x80000000UL;
102 unsigned long int mask = 0xffffffffUL;
105 s = 4357; /* the default seed is 4357 */
107 /* We use the congruence s_{n+1} = (69069*s_n) mod 2^32 to
108 initialize the state. This works because ANSI-C unsigned long
109 integer arithmetic is automatically modulo 2^32 (or a higher
110 power of two), so we can safely ignore overflow. */
112 #define LCG(n) ((69069 * n) & 0xffffffffUL)
114 /* Brian Gough suggests this to avoid low-order bit correlations */
115 for (i = 0; i <= M; i++)
117 unsigned long t = 0 ;
118 unsigned long bit = msb ;
119 for (j = 0; j < 32; j++)
129 /* Perform the "orthogonalization" of the matrix */
130 /* Based on the orthogonalization used in r250, as suggested initially
131 * by Kirkpatrick and Stoll, and pointed out to me by Brian Gough
134 /* BJG: note that this orthogonalisation doesn't have any effect
135 here because the the initial 6695 elements do not participate in
136 the calculation. For practical purposes this orthogonalisation
137 is somewhat irrelevant, because the probability of the original
138 sequence being degenerate should be exponentially small. */
140 for (i=0; i<32; ++i) {
142 state->ra[k] &= mask; /* Turn off bits left of the diagonal */
143 state->ra[k] |= msb; /* Turn on the diagonal bit */
151 static const gsl_rng_type gfsr4_type =
153 0xffffffffUL, /* RAND_MAX */
155 sizeof (gfsr4_state_t),
160 const gsl_rng_type *gsl_rng_gfsr4 = &gfsr4_type;