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 lagged fibonacci generator with skipping developed by Luescher.
25 The sequence is a series of 24-bit integers, x_n,
29 where d_n = x_{n-10} - x_{n-24} - c_{n-1}, b_n = 0 if d_n >= 0 and
30 b_n = 2^24 if d_n < 0, c_n = 0 if d_n >= 0 and c_n = 1 if d_n < 0,
31 where after 24 samples a group of p integers are "skipped", to
32 reduce correlations. By default p = 199, but can be increased to
35 The period of the generator is around 10^171.
37 From: M. Luescher, "A portable high-quality random number generator
38 for lattice field theory calculations", Computer Physics
39 Communications, 79 (1994) 100-110.
41 Available on the net as hep-lat/9309020 at http://xxx.lanl.gov/
45 F. James, "RANLUX: A Fortran implementation of the high-quality
46 pseudo-random number generator of Luscher", Computer Physics
47 Communications, 79 (1994) 111-114
49 Kenneth G. Hamilton, F. James, "Acceleration of RANLUX", Computer
50 Physics Communications, 101 (1997) 241-248
52 Kenneth G. Hamilton, "Assembler RANLUX for PCs", Computer Physics
53 Communications, 101 (1997) 249-253 */
55 static inline unsigned long int ranlux_get (void *vstate);
56 static double ranlux_get_double (void *vstate);
57 static void ranlux_set_lux (void *state, unsigned long int s, unsigned int luxury);
58 static void ranlux_set (void *state, unsigned long int s);
59 static void ranlux389_set (void *state, unsigned long int s);
61 static const unsigned long int mask_lo = 0x00ffffffUL; /* 2^24 - 1 */
62 static const unsigned long int mask_hi = ~0x00ffffffUL;
63 static const unsigned long int two24 = 16777216; /* 2^24 */
72 unsigned long int u[24];
76 static inline unsigned long int increment_state (ranlux_state_t * state);
78 static inline unsigned long int
79 increment_state (ranlux_state_t * state)
81 unsigned int i = state->i;
82 unsigned int j = state->j;
83 long int delta = state->u[j] - state->u[i] - state->carry;
122 static inline unsigned long int
123 ranlux_get (void *vstate)
125 ranlux_state_t *state = (ranlux_state_t *) vstate;
126 const unsigned int skip = state->skip;
127 unsigned long int r = increment_state (state);
135 for (i = 0; i < skip; i++)
136 increment_state (state);
143 ranlux_get_double (void *vstate)
145 return ranlux_get (vstate) / 16777216.0;
149 ranlux_set_lux (void *vstate, unsigned long int s, unsigned int luxury)
151 ranlux_state_t *state = (ranlux_state_t *) vstate;
157 s = 314159265; /* default seed is 314159265 */
161 /* This is the initialization algorithm of F. James, widely in use
164 for (i = 0; i < 24; i++)
166 unsigned long int k = seed / 53668;
167 seed = 40014 * (seed - k * 53668) - k * 12211;
172 state->u[i] = seed % two24;
178 state->skip = luxury - 24;
180 if (state->u[23] & mask_hi)
191 ranlux_set (void *vstate, unsigned long int s)
193 ranlux_set_lux (vstate, s, 223);
197 ranlux389_set (void *vstate, unsigned long int s)
199 ranlux_set_lux (vstate, s, 389);
203 static const gsl_rng_type ranlux_type =
204 {"ranlux", /* name */
205 0x00ffffffUL, /* RAND_MAX */
207 sizeof (ranlux_state_t),
212 static const gsl_rng_type ranlux389_type =
213 {"ranlux389", /* name */
214 0x00ffffffUL, /* RAND_MAX */
216 sizeof (ranlux_state_t),
221 const gsl_rng_type *gsl_rng_ranlux = &ranlux_type;
222 const gsl_rng_type *gsl_rng_ranlux389 = &ranlux389_type;