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 reincarnation of the infamously bad RANDU generator.
27 x_{n+1} = (a x_n) mod m
29 with a = 65539 and m = 2^31 = 2147483648. The seed specifies
30 the initial value, x_1.
32 The theoretical value of x_{10001} is 1623524161.
34 The period of this generator is 2^29.
36 Note: Knuth describes this generator as "really horrible".
38 From: Park and Miller, "Random Number Generators: Good ones are
39 hard to find" Communications of the ACM, October 1988, Volume 31,
40 No 10, pages 1192-1201. */
42 static inline unsigned long int randu_get (void *vstate);
43 static double randu_get_double (void *vstate);
44 static void randu_set (void *state, unsigned long int s);
46 static const long int a = 65539;
47 /* static const unsigned long int m = 2147483648UL; */
55 static inline unsigned long int
56 randu_get (void *vstate)
58 randu_state_t *state = (randu_state_t *) vstate;
60 /* The following line relies on unsigned 32-bit arithmetic */
62 state->x = (a * state->x) & 0x7fffffffUL;
68 randu_get_double (void *vstate)
70 return randu_get (vstate) / 2147483648.0 ;
74 randu_set (void *vstate, unsigned long int s)
76 randu_state_t *state = (randu_state_t *) vstate;
79 s = 1; /* default seed is 1 */
86 static const gsl_rng_type randu_type =
88 0x7fffffffUL, /* RAND_MAX */
90 sizeof (randu_state_t),
95 const gsl_rng_type *gsl_rng_randu = &randu_type;