3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 3 of the License, or (at
6 * your option) any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * This generator is taken from
22 * The Art of Computer Programming
28 * This implementation copyright (C) 2001 Carlo Perassi
29 * and (C) 2003 Heiko Bauke.
34 #include <gsl/gsl_rng.h>
38 #define AA1 271828183UL
39 #define AA2 1833324378UL /* = -314159269 mod (2 ^ 31 -1) */
40 #define MM 0x7fffffffUL /* 2 ^ 31 - 1 */
41 #define CEIL_SQRT_MM 46341UL /* sqrt(2 ^ 31 - 1) */
43 static inline unsigned long int ran_get (void *vstate);
44 static double ran_get_double (void *vstate);
45 static void ran_set (void *state, unsigned long int s);
54 static inline unsigned long int
55 ran_get (void *vstate)
57 ran_state_t *state = (ran_state_t *) vstate;
59 const unsigned long int xtmp = state->x1;
60 state->x1 = schrage_mult (AA1, state->x1, MM, CEIL_SQRT_MM)
61 + schrage_mult (AA2, state->x0, MM, CEIL_SQRT_MM);
72 ran_get_double (void *vstate)
74 ran_state_t *state = (ran_state_t *) vstate;
76 return ran_get (state) / 2147483647.0;
80 ran_set (void *vstate, unsigned long int s)
82 ran_state_t *state = (ran_state_t *) vstate;
85 s = 1; /* default seed is 1 */
93 static const gsl_rng_type ran_type = {
94 "knuthran2", /* name */
95 MM - 1L, /* RAND_MAX */
103 const gsl_rng_type *gsl_rng_knuthran2 = &ran_type;