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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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
25 * This code was adapted to glib by Sebastian Wilhelmi.
29 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
30 * file for a list of people on the GLib Team. See the ChangeLog
31 * files for a list of changes. These files are distributed with
32 * GLib at ftp://ftp.gtk.org/pub/gtk/.
46 #include "gthreadinit.h"
48 G_LOCK_DEFINE_STATIC (global_random);
49 static GRand* global_random = NULL;
51 /* Period parameters */
54 #define MATRIX_A 0x9908b0df /* constant vector a */
55 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
56 #define LOWER_MASK 0x7fffffff /* least significant r bits */
58 /* Tempering parameters */
59 #define TEMPERING_MASK_B 0x9d2c5680
60 #define TEMPERING_MASK_C 0xefc60000
61 #define TEMPERING_SHIFT_U(y) (y >> 11)
62 #define TEMPERING_SHIFT_S(y) (y << 7)
63 #define TEMPERING_SHIFT_T(y) (y << 15)
64 #define TEMPERING_SHIFT_L(y) (y >> 18)
67 get_random_version (void)
69 static gboolean initialized = FALSE;
70 static guint random_version;
74 const gchar *version_string = g_getenv ("G_RANDOM_VERSION");
75 if (!version_string || version_string[0] == '\000' ||
76 strcmp (version_string, "2.2") == 0)
78 else if (strcmp (version_string, "2.0") == 0)
82 g_warning ("Unknown G_RANDOM_VERSION \"%s\". Using version 2.2.",
89 return random_version;
92 /* This is called from g_thread_init(). It's used to
93 * initialize some static data in a threadsafe way.
96 _g_rand_thread_init (void)
98 (void)get_random_version ();
103 guint32 mt[N]; /* the array for the state vector */
108 * g_rand_new_with_seed:
109 * @seed: a value to initialize the random number generator.
111 * Creates a new random number generator initialized with @seed.
113 * Return value: the new #GRand.
116 g_rand_new_with_seed (guint32 seed)
118 GRand *rand = g_new0 (GRand, 1);
119 g_rand_set_seed (rand, seed);
126 * Creates a new random number generator initialized with a seed taken
127 * either from <filename>/dev/urandom</filename> (if existing) or from
128 * the current time (as a fallback).
130 * Return value: the new #GRand.
138 static gboolean dev_urandom_exists = TRUE;
140 if (dev_urandom_exists)
142 FILE* dev_urandom = fopen("/dev/urandom", "rb");
145 if (fread (&seed, sizeof (seed), 1, dev_urandom) != 1)
146 dev_urandom_exists = FALSE;
147 fclose (dev_urandom);
150 dev_urandom_exists = FALSE;
153 static gboolean dev_urandom_exists = FALSE;
156 if (!dev_urandom_exists)
158 g_get_current_time (&now);
159 seed = now.tv_sec ^ now.tv_usec;
162 return g_rand_new_with_seed (seed);
169 * Frees the memory allocated for the #GRand.
172 g_rand_free (GRand* rand)
174 g_return_if_fail (rand != NULL);
182 * @seed: a value to reinitialize the random number generator.
184 * Sets the seed for the random number generator #GRand to @seed.
187 g_rand_set_seed (GRand* rand, guint32 seed)
189 g_return_if_fail (rand != NULL);
191 switch (get_random_version ())
194 /* setting initial seeds to mt[N] using */
195 /* the generator Line 25 of Table 1 in */
196 /* [KNUTH 1981, The Art of Computer Programming */
197 /* Vol. 2 (2nd Ed.), pp102] */
199 if (seed == 0) /* This would make the PRNG procude only zeros */
200 seed = 0x6b842128; /* Just set it to another number */
203 for (rand->mti=1; rand->mti<N; rand->mti++)
204 rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]);
208 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
209 /* In the previous version (see above), MSBs of the */
210 /* seed affect only MSBs of the array mt[]. */
213 for (rand->mti=1; rand->mti<N; rand->mti++)
214 rand->mt[rand->mti] = 1812433253UL *
215 (rand->mt[rand->mti-1] ^ (rand->mt[rand->mti-1] >> 30)) + rand->mti;
218 g_assert_not_reached ();
226 * Returns the next random #guint32 from @rand_ equally distributed over
227 * the range [0..2^32-1].
229 * Return value: A random number.
232 g_rand_int (GRand* rand)
235 static const guint32 mag01[2]={0x0, MATRIX_A};
236 /* mag01[x] = x * MATRIX_A for x=0,1 */
238 g_return_val_if_fail (rand != NULL, 0);
240 if (rand->mti >= N) { /* generate N words at one time */
243 for (kk=0;kk<N-M;kk++) {
244 y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
245 rand->mt[kk] = rand->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
248 y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
249 rand->mt[kk] = rand->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
251 y = (rand->mt[N-1]&UPPER_MASK)|(rand->mt[0]&LOWER_MASK);
252 rand->mt[N-1] = rand->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
257 y = rand->mt[rand->mti++];
258 y ^= TEMPERING_SHIFT_U(y);
259 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
260 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
261 y ^= TEMPERING_SHIFT_L(y);
266 /* transform [0..2^32] -> [0..1] */
267 #define G_RAND_DOUBLE_TRANSFORM 2.3283064365386962890625e-10
272 * @begin: lower closed bound of the interval.
273 * @end: upper open bound of the interval.
275 * Returns the next random #gint32 from @rand_ equally distributed over
276 * the range [@begin..@end-1].
278 * Return value: A random number.
281 g_rand_int_range (GRand* rand, gint32 begin, gint32 end)
283 guint32 dist = end - begin;
286 g_return_val_if_fail (rand != NULL, begin);
287 g_return_val_if_fail (end > begin, begin);
289 switch (get_random_version ())
292 if (dist <= 0x10000L) /* 2^16 */
294 /* This method, which only calls g_rand_int once is only good
295 * for (end - begin) <= 2^16, because we only have 32 bits set
296 * from the one call to g_rand_int (). */
298 /* we are using (trans + trans * trans), because g_rand_int only
299 * covers [0..2^32-1] and thus g_rand_int * trans only covers
300 * [0..1-2^-32], but the biggest double < 1 is 1-2^-52.
303 gdouble double_rand = g_rand_int (rand) *
304 (G_RAND_DOUBLE_TRANSFORM +
305 G_RAND_DOUBLE_TRANSFORM * G_RAND_DOUBLE_TRANSFORM);
307 random = (gint32) (double_rand * dist);
311 /* Now we use g_rand_double_range (), which will set 52 bits for
312 us, so that it is safe to round and still get a decent
314 random = (gint32) g_rand_double_range (rand, 0, dist);
322 /* maxvalue is set to the predecessor of the greatest
323 * multiple of dist less or equal 2^32. */
325 if (dist <= 0x80000000u) /* 2^31 */
327 /* maxvalue = 2^32 - 1 - (2^32 % dist) */
328 guint32 leftover = (0x80000000u % dist) * 2;
329 if (leftover >= dist) leftover -= dist;
330 maxvalue = 0xffffffffu - leftover;
336 random = g_rand_int (rand);
337 while (random > maxvalue);
343 random = 0; /* Quiet GCC */
344 g_assert_not_reached ();
347 return begin + random;
354 * Returns the next random #gdouble from @rand_ equally distributed over
357 * Return value: A random number.
360 g_rand_double (GRand* rand)
362 /* We set all 52 bits after the point for this, not only the first
363 32. Thats why we need two calls to g_rand_int */
364 gdouble retval = g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM;
365 retval = (retval + g_rand_int (rand)) * G_RAND_DOUBLE_TRANSFORM;
367 /* The following might happen due to very bad rounding luck, but
368 * actually this should be more than rare, we just try again then */
370 return g_rand_double (rand);
376 * g_rand_double_range:
378 * @begin: lower closed bound of the interval.
379 * @end: upper open bound of the interval.
381 * Returns the next random #gdouble from @rand_ equally distributed over
382 * the range [@begin..@end).
384 * Return value: A random number.
387 g_rand_double_range (GRand* rand, gdouble begin, gdouble end)
389 return g_rand_double (rand) * (end - begin) + begin;
395 * Return a random #guint32 equally distributed over the range
398 * Return value: A random number.
404 G_LOCK (global_random);
406 global_random = g_rand_new ();
408 result = g_rand_int (global_random);
409 G_UNLOCK (global_random);
414 * g_random_int_range:
415 * @begin: lower closed bound of the interval.
416 * @end: upper open bound of the interval.
418 * Returns a random #gint32 equally distributed over the range
421 * Return value: A random number.
424 g_random_int_range (gint32 begin, gint32 end)
427 G_LOCK (global_random);
429 global_random = g_rand_new ();
431 result = g_rand_int_range (global_random, begin, end);
432 G_UNLOCK (global_random);
439 * Returns a random #gdouble equally distributed over the range [0..1).
441 * Return value: A random number.
444 g_random_double (void)
447 G_LOCK (global_random);
449 global_random = g_rand_new ();
451 result = g_rand_double (global_random);
452 G_UNLOCK (global_random);
457 * g_random_double_range:
458 * @begin: lower closed bound of the interval.
459 * @end: upper open bound of the interval.
461 * Returns a random #gdouble equally distributed over the range [@begin..@end).
463 * Return value: A random number.
466 g_random_double_range (gdouble begin, gdouble end)
469 G_LOCK (global_random);
471 global_random = g_rand_new ();
473 result = g_rand_double_range (global_random, begin, end);
474 G_UNLOCK (global_random);
480 * @seed: a value to reinitialize the global random number generator.
482 * Sets the seed for the global random number generator, which is used
483 * by the <function>g_random_*</function> functions, to @seed.
486 g_random_set_seed (guint32 seed)
488 G_LOCK (global_random);
490 global_random = g_rand_new_with_seed (seed);
492 g_rand_set_seed (global_random, seed);
493 G_UNLOCK (global_random);