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 fifth-order multiple recursive generator. The sequence is,
26 x_n = (a_1 x_{n-1} + a_5 x_{n-5}) mod m
28 with a_1 = 107374182, a_2 = a_3 = a_4 = 0, a_5 = 104480 and m = 2^31-1.
30 We initialize the generator with x_n = s_n MOD m for n = 1..5,
31 where s_n = (69069 * s_{n-1}) mod 2^32, and s_0 = s is the
34 NOTE: According to the paper the seeds must lie in the range [0,
35 2^31 - 2] with at least one non-zero value -- our seeding procedure
36 satisfies these constraints.
38 We then use 6 iterations of the generator to "warm up" the internal
41 With this initialization procedure the theoretical value of
42 z_{10006} is 2064828650 for s = 1. The subscript 10006 means (1)
43 seed the generator with s = 1, (2) do the 6 warm-up iterations
44 that are part of the seeding process, (3) then do 10000 actual
47 The period of this generator is about 2^155.
49 From: P. L'Ecuyer, F. Blouin, and R. Coutre, "A search for good
50 multiple recursive random number generators", ACM Transactions on
51 Modeling and Computer Simulation 3, 87-98 (1993). */
53 static inline unsigned long int mrg_get (void *vstate);
54 static double mrg_get_double (void *vstate);
55 static void mrg_set (void *state, unsigned long int s);
57 static const long int m = 2147483647;
58 static const long int a1 = 107374182, q1 = 20, r1 = 7;
59 static const long int a5 = 104480, q5 = 20554, r5 = 1727;
63 long int x1, x2, x3, x4, x5;
67 static inline unsigned long int
68 mrg_get (void *vstate)
70 mrg_state_t *state = (mrg_state_t *) vstate;
72 long int p1, h1, p5, h5;
75 p5 = a5 * (state->x5 - h5 * q5) - h5 * r5;
80 p1 = a1 * (state->x1 - h1 * q1) - h1 * r1;
84 state->x5 = state->x4;
85 state->x4 = state->x3;
86 state->x3 = state->x2;
87 state->x2 = state->x1;
98 mrg_get_double (void *vstate)
100 return mrg_get (vstate) / 2147483647.0 ;
105 mrg_set (void *vstate, unsigned long int s)
107 /* An entirely adhoc way of seeding! This does **not** come from
110 mrg_state_t *state = (mrg_state_t *) vstate;
113 s = 1; /* default seed is 1 */
115 #define LCG(n) ((69069 * n) & 0xffffffffUL)
127 /* "warm it up" with at least 5 calls to go through
140 static const gsl_rng_type mrg_type =
142 2147483646, /* RAND_MAX */
144 sizeof (mrg_state_t),
149 const gsl_rng_type *gsl_rng_mrg = &mrg_type;