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 an implementation of Martin Luescher's second generation
25 double-precision (48-bit) version of the RANLUX generator.
27 Thanks to Martin Luescher for providing information on this
32 static inline unsigned long int ranlxd_get (void *vstate);
33 static double ranlxd_get_double (void *vstate);
34 static void ranlxd_set_lux (void *state, unsigned long int s, unsigned int luxury);
35 static void ranlxd1_set (void *state, unsigned long int s);
36 static void ranlxd2_set (void *state, unsigned long int s);
38 static const int next[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0};
40 static const double one_bit = 1.0 / 281474976710656.0; /* 1/2^48 */
42 #define RANLUX_STEP(x1,x2,i1,i2,i3) \
43 x1=xdbl[i1] - xdbl[i2]; \
62 static inline void increment_state (ranlxd_state_t * state);
65 increment_state (ranlxd_state_t * state)
70 double *xdbl = state->xdbl;
71 double carry = state->carry;
72 unsigned int ir = state->ir;
73 unsigned int jr = state->jr;
75 for (k = 0; ir > 0; ++k)
77 y1 = xdbl[jr] - xdbl[ir];
93 kmax = state->pr - 12;
95 for (; k <= kmax; k += 12)
97 y1 = xdbl[7] - xdbl[0];
100 RANLUX_STEP (y2, y1, 8, 1, 0);
101 RANLUX_STEP (y3, y2, 9, 2, 1);
102 RANLUX_STEP (y1, y3, 10, 3, 2);
103 RANLUX_STEP (y2, y1, 11, 4, 3);
104 RANLUX_STEP (y3, y2, 0, 5, 4);
105 RANLUX_STEP (y1, y3, 1, 6, 5);
106 RANLUX_STEP (y2, y1, 2, 7, 6);
107 RANLUX_STEP (y3, y2, 3, 8, 7);
108 RANLUX_STEP (y1, y3, 4, 9, 8);
109 RANLUX_STEP (y2, y1, 5, 10, 9);
110 RANLUX_STEP (y3, y2, 6, 11, 10);
126 for (; k < kmax; ++k)
128 y1 = xdbl[jr] - xdbl[ir];
146 state->carry = carry;
149 static inline unsigned long int
150 ranlxd_get (void *vstate)
152 return ranlxd_get_double (vstate) * 4294967296.0; /* 2^32 */
156 ranlxd_get_double (void *vstate)
158 ranlxd_state_t *state = (ranlxd_state_t *) vstate;
162 state->ir = next[ir];
164 if (state->ir == state->ir_old)
165 increment_state (state);
167 return state->xdbl[state->ir];
171 ranlxd_set_lux (void *vstate, unsigned long int s, unsigned int luxury)
173 ranlxd_state_t *state = (ranlxd_state_t *) vstate;
175 int ibit, jbit, i, k, l, xbit[31];
181 s = 1; /* default seed is 1 */
185 i = seed & 0xFFFFFFFFUL;
187 for (k = 0; k < 31; ++k)
196 for (k = 0; k < 12; ++k)
200 for (l = 1; l <= 48; ++l)
202 y = (double) ((xbit[ibit] + 1) % 2);
204 xbit[ibit] = (xbit[ibit] + xbit[jbit]) % 2;
205 ibit = (ibit + 1) % 31;
206 jbit = (jbit + 1) % 31;
208 state->xdbl[k] = one_bit * x;
219 ranlxd1_set (void *vstate, unsigned long int s)
221 ranlxd_set_lux (vstate, s, 202);
225 ranlxd2_set (void *vstate, unsigned long int s)
227 ranlxd_set_lux (vstate, s, 397);
230 static const gsl_rng_type ranlxd1_type =
231 {"ranlxd1", /* name */
232 0xffffffffUL, /* RAND_MAX */
234 sizeof (ranlxd_state_t),
239 static const gsl_rng_type ranlxd2_type =
240 {"ranlxd2", /* name */
241 0xffffffffUL, /* RAND_MAX */
243 sizeof (ranlxd_state_t),
248 const gsl_rng_type *gsl_rng_ranlxd1 = &ranlxd1_type;
249 const gsl_rng_type *gsl_rng_ranlxd2 = &ranlxd2_type;