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 the algorithm used in Knuths's
25 subtractive generator, with the Numerical Recipe's ran3 paramters.
26 It is a subtractive lagged fibonnaci generator. */
28 static inline unsigned long int ran3_get (void *vstate);
29 static double ran3_get_double (void *vstate);
30 static void ran3_set (void *state, unsigned long int s);
32 #define M_BIG 1000000000
33 #define M_SEED 161803398
39 unsigned long int buffer[56];
43 static inline unsigned long int
44 ran3_get (void *vstate)
46 ran3_state_t *state = (ran3_state_t *) vstate;
59 j = state->buffer[state->x] - state->buffer[state->y];
64 state->buffer[state->x] = j;
70 ran3_get_double (void *vstate)
72 return ran3_get (vstate) / (double) M_BIG ;
76 ran3_set (void *vstate, unsigned long int s)
78 ran3_state_t *state = (ran3_state_t *) vstate;
83 s = 1; /* default seed is 1 */
85 j = (M_SEED - s) % M_BIG;
87 /* the zeroth element is never used, but we initialize it for
88 consistency between states */
92 state->buffer[55] = j;
95 for (i = 1; i < 55; i++)
97 int n = (21 * i) % 55;
102 j = state->buffer[n];
106 for (i1 = 0; i1 < 4; i1++)
108 for (i = 1; i < 56; i++)
110 long int t = state->buffer[i] - state->buffer[1 + (i + 30) % 55];
113 state->buffer[i] = t;
123 static const gsl_rng_type ran3_type =
125 M_BIG, /* RAND_MAX */
127 sizeof (ran3_state_t),
132 const gsl_rng_type *gsl_rng_ran3 = &ran3_type;