1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* Originally developed and coded by Makoto Matsumoto and Takuji
21 * Nishimura. Please mail <matumoto@math.keio.ac.jp>, if you're using
22 * code from this file in your own programs or libraries.
23 * Further information on the Mersenne Twister can be found at
24 * http://www.math.keio.ac.jp/~matumoto/emt.html
28 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
29 * file for a list of people on the GLib Team. See the ChangeLog
30 * files for a list of changes. These files are distributed with
31 * GLib at ftp://ftp.gtk.org/pub/gtk/.
38 G_LOCK_DEFINE_STATIC (global_random);
39 static GRand* global_random = NULL;
41 /* Period parameters */
44 #define MATRIX_A 0x9908b0df /* constant vector a */
45 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
46 #define LOWER_MASK 0x7fffffff /* least significant r bits */
48 /* Tempering parameters */
49 #define TEMPERING_MASK_B 0x9d2c5680
50 #define TEMPERING_MASK_C 0xefc60000
51 #define TEMPERING_SHIFT_U(y) (y >> 11)
52 #define TEMPERING_SHIFT_S(y) (y << 7)
53 #define TEMPERING_SHIFT_T(y) (y << 15)
54 #define TEMPERING_SHIFT_L(y) (y >> 18)
58 guint32 mt[N]; /* the array for the state vector */
60 gboolean have_next_normal;
65 g_rand_new_with_seed (guint32 seed)
67 GRand *rand = g_new0 (GRand, 1);
68 g_rand_set_seed (rand, seed);
77 FILE* dev_random = fopen("/dev/random", "rb");
81 if (fread (&seed, sizeof (seed), 1, dev_random) != 1)
86 /* Using /dev/random alone makes the seed computable for the
87 outside. This might pose security problems somewhere. This should
88 yield better values */
90 g_get_current_time (&now);
91 seed ^= now.tv_sec ^ now.tv_usec;
93 return g_rand_new_with_seed (seed);
97 g_rand_free (GRand* rand)
99 g_return_if_fail (rand);
105 g_rand_set_seed (GRand* rand, guint32 seed)
107 g_return_if_fail (rand);
109 /* setting initial seeds to mt[N] using */
110 /* the generator Line 25 of Table 1 in */
111 /* [KNUTH 1981, The Art of Computer Programming */
112 /* Vol. 2 (2nd Ed.), pp102] */
113 rand->mt[0]= seed & 0xffffffff;
114 for (rand->mti=1; rand->mti<N; rand->mti++)
115 rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]) & 0xffffffff;
117 rand->have_next_normal = FALSE;
121 g_rand_int (GRand* rand)
124 static const guint32 mag01[2]={0x0, MATRIX_A};
125 /* mag01[x] = x * MATRIX_A for x=0,1 */
127 g_return_val_if_fail (rand, 0);
129 if (rand->mti >= N) { /* generate N words at one time */
132 for (kk=0;kk<N-M;kk++) {
133 y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
134 rand->mt[kk] = rand->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
137 y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
138 rand->mt[kk] = rand->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
140 y = (rand->mt[N-1]&UPPER_MASK)|(rand->mt[0]&LOWER_MASK);
141 rand->mt[N-1] = rand->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
146 y = rand->mt[rand->mti++];
147 y ^= TEMPERING_SHIFT_U(y);
148 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
149 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
150 y ^= TEMPERING_SHIFT_L(y);
156 g_rand_int_range (GRand* rand, gint32 min, gint32 max)
158 guint32 dist = max - min;
161 g_return_val_if_fail (rand, min);
162 g_return_val_if_fail (max > min, min);
164 if (dist <= 0x10000L) /* 2^16 */
166 /* All tricks doing modulo calculations do not have a good
167 distribution -> We must use this slower method for maximal
168 quality, but this method is only good for (max - min) <= 2^16 */
170 random = (gint32) g_rand_double_range (rand, 0, dist);
171 /* we'd rather use the following, if -lm is allowed later on:
172 random = (gint32) floor (g_rand_double_range (rand, 0, dist)); */
176 /* Now it's harder to make it right. We calculate the smallest m,
177 such that dist < 2 ^ m, then we calculate a random number in
178 [1..2^32-1] and rightshift it by 32 - m. Then we test, if it
179 is smaller than dist and if not, get a new number and so
180 forth until we get a number smaller than dist. We just return
182 guint32 border = 0x20000L; /* 2^17 */
183 guint right_shift = 15; /* 32 - 17 */
185 if (dist >= 0x80000000) /* in the case of dist > 2^31 our loop
186 below will be infinite */
192 while (dist >= border)
200 random = g_rand_int (rand) >> right_shift;
201 } while (random >= dist);
206 /* transform [0..2^32-1] -> [0..1) */
207 #define G_RAND_DOUBLE_TRANSFORM 2.3283064365386963e-10
210 g_rand_double (GRand* rand)
212 return g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM;
216 g_rand_double_range (GRand* rand, gdouble min, gdouble max)
218 return g_rand_int (rand) * ((max - min) * G_RAND_DOUBLE_TRANSFORM) + min;
222 #if WE_REALLY_WANT_HAVE_MATH_LIB_LINKED
224 g_rand_normal (GRand* rand, gdouble mean, gdouble standard_deviation)
226 /* For a description of the used algorithm see Knuth: "The Art of
227 Computer Programming", Vol.2, Second Edition, Page 117: Polar
228 method for normal deviates due to Box, Muller, Marsaglia */
230 g_return_val_if_fail (rand, 0);
232 if (rand->have_next_normal)
234 rand->have_next_normal = FALSE;
235 normal = rand->next_normal;
240 gdouble u2 = g_rand_double_range (rand, -1, 1);
245 u2 = g_rand_double_range (rand, -1, 1);
246 s = u1 * u1 + u2 * u2;
248 f = sqrt (-2 * log (s) / s);
250 rand->next_normal = u2 * f;
251 rand->have_next_normal = TRUE;
253 return mean + normal * standard_deviation;
261 G_LOCK (global_random);
263 global_random = g_rand_new ();
265 result = g_rand_int (global_random);
266 G_UNLOCK (global_random);
271 g_random_int_range (gint32 min, gint32 max)
274 G_LOCK (global_random);
276 global_random = g_rand_new ();
278 result = g_rand_int_range (global_random, min, max);
279 G_UNLOCK (global_random);
284 g_random_double (void)
287 G_LOCK (global_random);
289 global_random = g_rand_new ();
291 result = g_rand_double (global_random);
292 G_UNLOCK (global_random);
297 g_random_double_range (gdouble min, gdouble max)
300 G_LOCK (global_random);
302 global_random = g_rand_new ();
304 result = g_rand_double_range (global_random, min, max);
305 G_UNLOCK (global_random);
309 #if WE_REALLY_WANT_HAVE_MATH_LIB_LINKED
311 g_random_normal (gdouble mean, gdouble standard_deviation)
314 G_LOCK (global_random);
316 global_random = g_rand_new ();
318 result = g_rand_normal (global_random, mean, standard_deviation);
319 G_UNLOCK (global_random);
325 g_random_set_seed (guint32 seed)
327 G_LOCK (global_random);
329 global_random = g_rand_new_with_seed (seed);
331 g_rand_set_seed (global_random, seed);
332 G_UNLOCK (global_random);