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 Numerical
25 Recipe's ran1 generator. It is MINSTD with a 32-element
28 static inline unsigned long int ran1_get (void *vstate);
29 static double ran1_get_double (void *vstate);
30 static void ran1_set (void *state, unsigned long int s);
32 static const long int m = 2147483647, a = 16807, q = 127773, r = 2836;
35 #define N_DIV (1 + 2147483646/N_SHUFFLE)
41 unsigned long int shuffle[N_SHUFFLE];
45 static inline unsigned long int
46 ran1_get (void *vstate)
48 ran1_state_t *state = (ran1_state_t *) vstate;
50 const unsigned long int x = state->x;
52 const long int h = x / q;
53 const long int t = a * (x - h * q) - h * r;
65 unsigned long int j = state->n / N_DIV;
66 state->n = state->shuffle[j];
67 state->shuffle[j] = state->x;
74 ran1_get_double (void *vstate)
76 float x_max = 1 - 1.2e-7f ; /* Numerical Recipes version of 1-FLT_EPS */
78 float x = ran1_get (vstate) / 2147483647.0f ;
88 ran1_set (void *vstate, unsigned long int s)
90 ran1_state_t *state = (ran1_state_t *) vstate;
94 s = 1; /* default seed is 1 */
96 for (i = 0; i < 8; i++)
99 long int t = a * (s - h * q) - h * r;
105 for (i = N_SHUFFLE - 1; i >= 0; i--)
108 long int t = a * (s - h * q) - h * r;
112 state->shuffle[i] = s;
121 static const gsl_rng_type ran1_type =
123 2147483646, /* RAND_MAX */
125 sizeof (ran1_state_t),
130 const gsl_rng_type *gsl_rng_ran1 = &ran1_type;