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
25 * This code was adapted to glib by Sebastian Wilhelmi <wilhelmi@ira.uka.de>.
29 * Modified by the GLib Team and others 1997-1999. 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/.
43 G_LOCK_DEFINE_STATIC (global_random);
44 static GRand* global_random = NULL;
46 /* Period parameters */
49 #define MATRIX_A 0x9908b0df /* constant vector a */
50 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
51 #define LOWER_MASK 0x7fffffff /* least significant r bits */
53 /* Tempering parameters */
54 #define TEMPERING_MASK_B 0x9d2c5680
55 #define TEMPERING_MASK_C 0xefc60000
56 #define TEMPERING_SHIFT_U(y) (y >> 11)
57 #define TEMPERING_SHIFT_S(y) (y << 7)
58 #define TEMPERING_SHIFT_T(y) (y << 15)
59 #define TEMPERING_SHIFT_L(y) (y >> 18)
63 guint32 mt[N]; /* the array for the state vector */
68 g_rand_new_with_seed (guint32 seed)
70 GRand *rand = g_new0 (GRand, 1);
71 g_rand_set_seed (rand, seed);
80 static gboolean dev_urandom_exists = TRUE;
82 if (dev_urandom_exists)
84 FILE* dev_urandom = fopen("/dev/urandom", "rb");
87 if (fread (&seed, sizeof (seed), 1, dev_urandom) != 1)
88 dev_urandom_exists = FALSE;
92 dev_urandom_exists = FALSE;
94 if (!dev_urandom_exists)
96 g_get_current_time (&now);
97 seed = now.tv_sec ^ now.tv_usec;
100 return g_rand_new_with_seed (seed);
104 g_rand_free (GRand* rand)
106 g_return_if_fail (rand != NULL);
112 g_rand_set_seed (GRand* rand, guint32 seed)
114 g_return_if_fail (rand != NULL);
116 /* setting initial seeds to mt[N] using */
117 /* the generator Line 25 of Table 1 in */
118 /* [KNUTH 1981, The Art of Computer Programming */
119 /* Vol. 2 (2nd Ed.), pp102] */
121 if (seed == 0) /* This would make the PRNG procude only zeros */
122 seed = 0x6b842128; /* Just set it to another number */
124 rand->mt[0]= seed & 0xffffffff;
125 for (rand->mti=1; rand->mti<N; rand->mti++)
126 rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]) & 0xffffffff;
130 g_rand_int (GRand* rand)
133 static const guint32 mag01[2]={0x0, MATRIX_A};
134 /* mag01[x] = x * MATRIX_A for x=0,1 */
136 g_return_val_if_fail (rand != NULL, 0);
138 if (rand->mti >= N) { /* generate N words at one time */
141 for (kk=0;kk<N-M;kk++) {
142 y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
143 rand->mt[kk] = rand->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
146 y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
147 rand->mt[kk] = rand->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
149 y = (rand->mt[N-1]&UPPER_MASK)|(rand->mt[0]&LOWER_MASK);
150 rand->mt[N-1] = rand->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
155 y = rand->mt[rand->mti++];
156 y ^= TEMPERING_SHIFT_U(y);
157 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
158 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
159 y ^= TEMPERING_SHIFT_L(y);
165 g_rand_int_range (GRand* rand, gint32 min, gint32 max)
167 guint32 dist = max - min;
170 g_return_val_if_fail (rand != NULL, min);
171 g_return_val_if_fail (max > min, min);
173 if (dist <= 0x10000L) /* 2^16 */
175 /* All tricks doing modulo calculations do not have a good
176 distribution -> We must use this slower method for maximal
177 quality, but this method is only good for (max - min) <= 2^16 */
179 random = (gint32) g_rand_double_range (rand, 0, dist);
180 /* we'd rather use the following, if -lm is allowed later on:
181 random = (gint32) floor (g_rand_double_range (rand, 0, dist)); */
185 /* Now it's harder to make it right. We calculate the smallest m,
186 such that dist < 2 ^ m, then we calculate a random number in
187 [1..2^32-1] and rightshift it by 32 - m. Then we test, if it
188 is smaller than dist and if not, get a new number and so
189 forth until we get a number smaller than dist. We just return
191 guint32 border = 0x20000L; /* 2^17 */
192 guint right_shift = 15; /* 32 - 17 */
194 if (dist >= 0x80000000) /* in the case of dist > 2^31 our loop
195 below will be infinite */
201 while (dist >= border)
209 random = g_rand_int (rand) >> right_shift;
210 } while (random >= dist);
215 /* transform [0..2^32-1] -> [0..1) */
216 #define G_RAND_DOUBLE_TRANSFORM 2.3283064365386963e-10
219 g_rand_double (GRand* rand)
221 return g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM;
225 g_rand_double_range (GRand* rand, gdouble min, gdouble max)
227 return g_rand_int (rand) * ((max - min) * G_RAND_DOUBLE_TRANSFORM) + min;
234 G_LOCK (global_random);
236 global_random = g_rand_new ();
238 result = g_rand_int (global_random);
239 G_UNLOCK (global_random);
244 g_random_int_range (gint32 min, gint32 max)
247 G_LOCK (global_random);
249 global_random = g_rand_new ();
251 result = g_rand_int_range (global_random, min, max);
252 G_UNLOCK (global_random);
257 g_random_double (void)
260 G_LOCK (global_random);
262 global_random = g_rand_new ();
264 result = g_rand_double (global_random);
265 G_UNLOCK (global_random);
270 g_random_double_range (gdouble min, gdouble max)
273 G_LOCK (global_random);
275 global_random = g_rand_new ();
277 result = g_rand_double_range (global_random, min, max);
278 G_UNLOCK (global_random);
283 g_random_set_seed (guint32 seed)
285 G_LOCK (global_random);
287 global_random = g_rand_new_with_seed (seed);
289 g_rand_set_seed (global_random, seed);
290 G_UNLOCK (global_random);