1 /* rng/ran3.c
2 *
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007, 2010 James Theiler, Brian Gough
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <gsl/gsl_rng.h>
23
24 /* This is an implementation of the algorithm used in Knuths's
25 subtractive generator, with the Numerical Recipe's ran3 paramters.
26 It is a subtractive lagged fibonnaci generator. */
27
28 static inline unsigned long int ran3_get (void *vstate);
29 static double ran3_get_double (void *vstate);
30 static void ran3_set (void *state, unsigned long int s);
31
32 #define M_BIG 1000000000
33 #define M_SEED 161803398
34
35 typedef struct
36 {
37 unsigned int x;
38 unsigned int y;
39 unsigned long int buffer[56];
40 }
41 ran3_state_t;
42
43 static inline unsigned long int
ran3_get(void * vstate)44 ran3_get (void *vstate)
45 {
46 ran3_state_t *state = (ran3_state_t *) vstate;
47 long int j;
48
49 state->x++;
50
51 if (state->x == 56)
52 state->x = 1;
53
54 state->y++;
55
56 if (state->y == 56)
57 state->y = 1;
58
59 j = state->buffer[state->x] - state->buffer[state->y];
60
61 if (j < 0)
62 j += M_BIG;
63
64 state->buffer[state->x] = j;
65
66 return j;
67 }
68
69 static double
ran3_get_double(void * vstate)70 ran3_get_double (void *vstate)
71 {
72 return ran3_get (vstate) / (double) M_BIG ;
73 }
74
75 static void
ran3_set(void * vstate,unsigned long int s)76 ran3_set (void *vstate, unsigned long int s)
77 {
78 ran3_state_t *state = (ran3_state_t *) vstate;
79 int i, i1;
80 long int j, k;
81
82 if (s == 0)
83 s = 1; /* default seed is 1 */
84
85 j = (M_SEED - s) % M_BIG;
86
87 /* Avoid potential problem with negative values */
88 if (j < 0) j += M_BIG;
89
90 /* the zeroth element is never used, but we initialize it for
91 consistency between states */
92
93 state->buffer[0] = 0;
94
95 state->buffer[55] = j;
96
97 k = 1;
98 for (i = 1; i < 55; i++)
99 {
100 int n = (21 * i) % 55;
101 state->buffer[n] = k;
102 k = j - k;
103 if (k < 0)
104 k += M_BIG;
105 j = state->buffer[n];
106
107 }
108
109 for (i1 = 0; i1 < 4; i1++)
110 {
111 for (i = 1; i < 56; i++)
112 {
113 long int t = state->buffer[i] - state->buffer[1 + (i + 30) % 55];
114 if (t < 0)
115 t += M_BIG;
116 state->buffer[i] = t;
117 }
118 }
119
120 state->x = 0;
121 state->y = 31;
122
123 return;
124 }
125
126 static const gsl_rng_type ran3_type =
127 {"ran3", /* name */
128 M_BIG, /* RAND_MAX */
129 0, /* RAND_MIN */
130 sizeof (ran3_state_t),
131 &ran3_set,
132 &ran3_get,
133 &ran3_get_double};
134
135 const gsl_rng_type *gsl_rng_ran3 = &ran3_type;
136