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 /* It is crucial that m == n-273 mod 607 at all times;
25 For speed of execution, however, this is never enforced.
26 Instead is is set in the initializer: note 607-273=334
27 Note also that the state.u[607] is not initialized */
29 static inline unsigned long int zuf_get (void *vstate);
30 static double zuf_get_double (void *vstate);
31 static void zuf_set (void *state, unsigned long int s);
33 static const unsigned long int zuf_randmax = 16777216; /* 2^24 */
38 unsigned long int u[607];
42 /* The zufall package was implemented with float's, which is to say 24
43 bits of precision. Since I'm using long's instead, my RANDMAX
46 static inline unsigned long int
47 zuf_get (void *vstate)
49 zuf_state_t *state = (zuf_state_t *) vstate;
50 const int n = state->n;
51 const int m = (n - 273 + 607) % 607;
52 unsigned long int t = state->u[n] + state->u[m];
54 while (t > zuf_randmax)
72 zuf_get_double (void *vstate)
74 return zuf_get (vstate) / 16777216.0 ;
78 zuf_set (void *vstate, unsigned long int s)
80 /* A very elaborate seeding procedure is provided with the
81 zufall package; this is virtually a copy of that procedure */
83 /* Initialized data */
89 long int i, j, k, l, m;
93 zuf_state_t *state = (zuf_state_t *) vstate;
97 /* generates initial seed buffer by linear congruential */
98 /* method. Taken from Marsaglia, FSU report FSU-SCRI-87-50 */
99 /* variable seed should be 0 < seed <31328 */
102 s = 1802; /* default seed is 1802 */
106 i = ij / 177 % 177 + 2;
108 k = kl / 169 % 178 + 1;
110 for (ii = 0; ii < 607; ++ii)
115 for (jj = 1; jj <= 24; ++jj)
117 m = i * j % 179 * k % 179;
121 l = (l * 53 + 1) % 169;
122 if (l * m % 64 >= 32)
128 state->u[ii] = (unsigned long int) (x * zuf_randmax);
132 static const gsl_rng_type zuf_type =
134 0x00ffffffUL, /* RAND_MAX */
136 sizeof (zuf_state_t),
141 const gsl_rng_type *gsl_rng_zuf = &zuf_type;