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 the TT800 twisted GSFR generator for 32 bit integers. It
25 has been superceded by MT19937 (mt.c). The period is 2^800.
27 This implementation is based on tt800.c, July 8th 1996 version by
28 M. Matsumoto, email: matumoto@math.keio.ac.jp
30 From: Makoto Matsumoto and Yoshiharu Kurita, "Twisted GFSR
31 Generators II", ACM Transactions on Modelling and Computer
32 Simulation, Vol. 4, No. 3, 1994, pages 254-266. */
34 static inline unsigned long int tt_get (void *vstate);
35 static double tt_get_double (void *vstate);
36 static void tt_set (void *state, unsigned long int s);
44 unsigned long int x[N];
48 static inline unsigned long int
51 tt_state_t *state = (tt_state_t *) vstate;
53 /* this is the magic vector, a */
55 const unsigned long mag01[2] =
56 {0x00000000, 0x8ebfd028UL};
58 unsigned long int *const x = state->x;
64 for (i = 0; i < N - M; i++)
66 x[i] = x[i + M] ^ (x[i] >> 1) ^ mag01[x[i] % 2];
70 x[i] = x[i + (M - N)] ^ (x[i] >> 1) ^ mag01[x[i] % 2];
76 y ^= (y << 7) & 0x2b5b2500UL; /* s and b, magic vectors */
77 y ^= (y << 15) & 0xdb8b0000UL; /* t and c, magic vectors */
78 y &= 0xffffffffUL; /* you may delete this line if word size = 32 */
80 /* The following line was added by Makoto Matsumoto in the 1996
81 version to improve lower bit's correlation. Delete this line
82 to use the code published in 1994. */
84 y ^= (y >> 16); /* added to the 1994 version */
92 tt_get_double (void * vstate)
94 return tt_get (vstate) / 4294967296.0 ;
98 tt_set (void *vstate, unsigned long int s)
100 tt_state_t *state = (tt_state_t *) vstate;
102 const tt_state_t init_state =
104 {0x95f24dabUL, 0x0b685215UL, 0xe76ccae7UL,
105 0xaf3ec239UL, 0x715fad23UL, 0x24a590adUL,
106 0x69e4b5efUL, 0xbf456141UL, 0x96bc1b7bUL,
107 0xa7bdf825UL, 0xc1de75b7UL, 0x8858a9c9UL,
108 0x2da87693UL, 0xb657f9ddUL, 0xffdc8a9fUL,
109 0x8121da71UL, 0x8b823ecbUL, 0x885d05f5UL,
110 0x4e20cd47UL, 0x5a9ad5d9UL, 0x512c0c03UL,
111 0xea857ccdUL, 0x4cc1d30fUL, 0x8891a8a1UL,
115 if (s == 0) /* default seed is given explicitly in the original code */
125 state->x[0] = s & 0xffffffffUL;
127 for (i = 1; i < N; i++)
128 state->x[i] = (69069 * state->x[i - 1]) & 0xffffffffUL;
134 static const gsl_rng_type tt_type =
136 0xffffffffUL, /* RAND_MAX */
143 const gsl_rng_type *gsl_rng_tt800 = &tt_type;