* code from this file in your own programs or libraries.
* Further information on the Mersenne Twister can be found at
* http://www.math.keio.ac.jp/~matumoto/emt.html
+ * This code was adapted to glib by Sebastian Wilhelmi <wilhelmi@ira.uka.de>.
*/
/*
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
+/*
+ * MT safe
+ */
+
#include <glib.h>
#include <math.h>
#include <stdio.h>
}
GRand*
-g_rand_new ()
+g_rand_new (void)
{
guint32 seed = 0;
GTimeVal now;
- FILE* dev_random = fopen("/dev/random", "rb");
-
- if (dev_random)
+ static gboolean dev_random_exists = TRUE;
+
+ if (dev_random_exists)
{
- if (fread (&seed, sizeof (seed), 1, dev_random) != 1)
- seed = 0;
- fclose (dev_random);
+ FILE* dev_random = fopen("/dev/random", "rb");
+ if (dev_random)
+ {
+ if (fread (&seed, sizeof (seed), 1, dev_random) != 1)
+ seed = 0;
+ else
+ dev_random_exists = FALSE;
+ fclose (dev_random);
+ }
+ else
+ dev_random_exists = FALSE;
}
/* Using /dev/random alone makes the seed computable for the
void
g_rand_free (GRand* rand)
{
- g_return_if_fail (rand);
+ g_return_if_fail (rand != NULL);
g_free (rand);
}
void
g_rand_set_seed (GRand* rand, guint32 seed)
{
- g_return_if_fail (rand);
+ g_return_if_fail (rand != NULL);
/* setting initial seeds to mt[N] using */
/* the generator Line 25 of Table 1 in */
static const guint32 mag01[2]={0x0, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
- g_return_val_if_fail (rand, 0);
+ g_return_val_if_fail (rand != NULL, 0);
if (rand->mti >= N) { /* generate N words at one time */
int kk;
guint32 dist = max - min;
guint32 random;
- g_return_val_if_fail (rand, min);
+ g_return_val_if_fail (rand != NULL, min);
g_return_val_if_fail (max > min, min);
if (dist <= 0x10000L) /* 2^16 */
Computer Programming", Vol.2, Second Edition, Page 117: Polar
method for normal deviates due to Box, Muller, Marsaglia */
gdouble normal;
- g_return_val_if_fail (rand, 0);
+ g_return_val_if_fail (rand != NULL, 0);
if (rand->have_next_normal)
{