17 static void r250_init(
int seed);
19 static double dr250(
void);
28 static long set_seed(
long);
30 static unsigned long int randlcg(
void);
88 static int nb_call = 0;
90 if (nb_call == 0) { r250_init(0); }
94 x1 = 2.0 * dr250() - 1.0;
95 x2 = 2.0 * dr250() - 1.0;
96 w = x1 * x1 + x2 * x2;
99 w = sqrt((-2.0 * log(w)) / w);
106 #include <gsl/gsl_rng.h>
107 #include <gsl/gsl_randist.h>
111 static gsl_rng *r = NULL;
113 if (!r) { r = gsl_rng_alloc(gsl_rng_mt19937); }
114 return gsl_ran_gaussian(r, 1.);
143 #define MSB 0x40000000L
144 #define ALL_BITS 0x7fffffffL
145 #define HALF_RANGE 0x20000000L
150 #define MSB 0x80000000L
151 #define ALL_BITS 0xffffffffL
152 #define HALF_RANGE 0x40000000L
158 #define ALL_BITS 0xffff
159 #define HALF_RANGE 0x4000
163 static unsigned int r250_buffer[ 250 ];
164 static int r250_index;
166 static void r250_init(
int sd)
169 unsigned int mask, msb;
173 for (j = 0; j < 250; j++) {
174 r250_buffer[j] = randlcg();
177 for (j = 0; j < 250; j++)
178 if (randlcg() > HALF_RANGE) {
179 r250_buffer[j] |= MSB;
185 for (j = 0; j < BITS; j++) {
187 r250_buffer[k] &= mask;
188 r250_buffer[k] |= msb;
197 static unsigned int r250(
void)
200 register unsigned int new_rand;
202 if (r250_index >= 147) {
203 j = r250_index - 147;
205 j = r250_index + 103;
208 new_rand = r250_buffer[ r250_index ] ^ r250_buffer[ j ];
209 r250_buffer[ r250_index ] = new_rand;
211 if (r250_index >= 249) {
223 static double dr250(
void)
226 register unsigned int new_rand;
228 if (r250_index >= 147) {
229 j = r250_index - 147;
231 j = r250_index + 103;
234 new_rand = r250_buffer[ r250_index ] ^ r250_buffer[ j ];
235 r250_buffer[ r250_index ] = new_rand;
237 if (r250_index >= 249) {
243 return (
double)new_rand / ALL_BITS;
254 static long int quotient = LONG_MAX / 16807L;
255 static long int my_remainder = LONG_MAX % 16807L;
257 static long int seed_val = 1L;
259 static long set_seed(
long int sd)
261 return seed_val = sd;
269 unsigned long int randlcg()
271 if (seed_val <= quotient) {
272 seed_val = (seed_val * 16807L) % LONG_MAX;
274 long int high_part = seed_val / quotient;
275 long int low_part = seed_val % quotient;
277 long int test = 16807L * low_part - my_remainder * high_part;
282 seed_val = test + LONG_MAX;