Moved struct declaration up. Style fixes.

1999-04-12  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>

	* glib.h: Moved struct declaration up. Style fixes.

	* grand.c: Style fixes. Only try to open /dev/random once.

	* tests/rand-test.c (main): New tests; Slight bug fix.
This commit is contained in:
Sebastian Wilhelmi 1999-04-12 12:53:37 +00:00 committed by Sebastian Wilhelmi
parent cb33239290
commit 3aa65d30eb
13 changed files with 140 additions and 35 deletions

View File

@ -1,3 +1,11 @@
1999-04-12 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h: Moved struct declaration up. Style fixes.
* grand.c: Style fixes. Only try to open /dev/random once.
* tests/rand-test.c (main): New tests; Slight bug fix.
1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* grand.c, tests/rand-test.c: New files to implement the Mersenne * grand.c, tests/rand-test.c: New files to implement the Mersenne

View File

@ -1,3 +1,11 @@
1999-04-12 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h: Moved struct declaration up. Style fixes.
* grand.c: Style fixes. Only try to open /dev/random once.
* tests/rand-test.c (main): New tests; Slight bug fix.
1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* grand.c, tests/rand-test.c: New files to implement the Mersenne * grand.c, tests/rand-test.c: New files to implement the Mersenne

View File

@ -1,3 +1,11 @@
1999-04-12 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h: Moved struct declaration up. Style fixes.
* grand.c: Style fixes. Only try to open /dev/random once.
* tests/rand-test.c (main): New tests; Slight bug fix.
1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* grand.c, tests/rand-test.c: New files to implement the Mersenne * grand.c, tests/rand-test.c: New files to implement the Mersenne

View File

@ -1,3 +1,11 @@
1999-04-12 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h: Moved struct declaration up. Style fixes.
* grand.c: Style fixes. Only try to open /dev/random once.
* tests/rand-test.c (main): New tests; Slight bug fix.
1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* grand.c, tests/rand-test.c: New files to implement the Mersenne * grand.c, tests/rand-test.c: New files to implement the Mersenne

View File

@ -1,3 +1,11 @@
1999-04-12 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h: Moved struct declaration up. Style fixes.
* grand.c: Style fixes. Only try to open /dev/random once.
* tests/rand-test.c (main): New tests; Slight bug fix.
1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* grand.c, tests/rand-test.c: New files to implement the Mersenne * grand.c, tests/rand-test.c: New files to implement the Mersenne

View File

@ -1,3 +1,11 @@
1999-04-12 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h: Moved struct declaration up. Style fixes.
* grand.c: Style fixes. Only try to open /dev/random once.
* tests/rand-test.c (main): New tests; Slight bug fix.
1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* grand.c, tests/rand-test.c: New files to implement the Mersenne * grand.c, tests/rand-test.c: New files to implement the Mersenne

View File

@ -1,3 +1,11 @@
1999-04-12 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h: Moved struct declaration up. Style fixes.
* grand.c: Style fixes. Only try to open /dev/random once.
* tests/rand-test.c (main): New tests; Slight bug fix.
1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* grand.c, tests/rand-test.c: New files to implement the Mersenne * grand.c, tests/rand-test.c: New files to implement the Mersenne

View File

@ -1,3 +1,11 @@
1999-04-12 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h: Moved struct declaration up. Style fixes.
* grand.c: Style fixes. Only try to open /dev/random once.
* tests/rand-test.c (main): New tests; Slight bug fix.
1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 1999-04-09 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* grand.c, tests/rand-test.c: New files to implement the Mersenne * grand.c, tests/rand-test.c: New files to implement the Mersenne

8
glib.h
View File

@ -698,6 +698,7 @@ typedef struct _GMemChunk GMemChunk;
typedef struct _GNode GNode; typedef struct _GNode GNode;
typedef struct _GPtrArray GPtrArray; typedef struct _GPtrArray GPtrArray;
typedef struct _GQueue GQueue; typedef struct _GQueue GQueue;
typedef struct _GRand GRand;
typedef struct _GRelation GRelation; typedef struct _GRelation GRelation;
typedef struct _GScanner GScanner; typedef struct _GScanner GScanner;
typedef struct _GScannerConfig GScannerConfig; typedef struct _GScannerConfig GScannerConfig;
@ -2378,9 +2379,8 @@ gpointer g_tuples_index (GTuples *tuples,
* double_range -> [min..max) * double_range -> [min..max)
*/ */
typedef struct _GRand GRand;
GRand* g_rand_new_with_seed (guint32 seed); GRand* g_rand_new_with_seed (guint32 seed);
GRand* g_rand_new (); GRand* g_rand_new (void);
void g_rand_free (GRand *rand); void g_rand_free (GRand *rand);
void g_rand_set_seed (GRand *rand, void g_rand_set_seed (GRand *rand,
@ -2400,10 +2400,10 @@ gdouble g_rand_normal (GRand *rand,
*/ */
void g_random_set_seed (guint32 seed); void g_random_set_seed (guint32 seed);
guint32 g_random_int (); guint32 g_random_int (void);
gint32 g_random_int_range (gint32 min, gint32 g_random_int_range (gint32 min,
gint32 max); gint32 max);
gdouble g_random_double (); gdouble g_random_double (void);
gdouble g_random_double_range (gdouble min, gdouble g_random_double_range (gdouble min,
gdouble max); gdouble max);
/* dito /* dito

View File

@ -698,6 +698,7 @@ typedef struct _GMemChunk GMemChunk;
typedef struct _GNode GNode; typedef struct _GNode GNode;
typedef struct _GPtrArray GPtrArray; typedef struct _GPtrArray GPtrArray;
typedef struct _GQueue GQueue; typedef struct _GQueue GQueue;
typedef struct _GRand GRand;
typedef struct _GRelation GRelation; typedef struct _GRelation GRelation;
typedef struct _GScanner GScanner; typedef struct _GScanner GScanner;
typedef struct _GScannerConfig GScannerConfig; typedef struct _GScannerConfig GScannerConfig;
@ -2378,9 +2379,8 @@ gpointer g_tuples_index (GTuples *tuples,
* double_range -> [min..max) * double_range -> [min..max)
*/ */
typedef struct _GRand GRand;
GRand* g_rand_new_with_seed (guint32 seed); GRand* g_rand_new_with_seed (guint32 seed);
GRand* g_rand_new (); GRand* g_rand_new (void);
void g_rand_free (GRand *rand); void g_rand_free (GRand *rand);
void g_rand_set_seed (GRand *rand, void g_rand_set_seed (GRand *rand,
@ -2400,10 +2400,10 @@ gdouble g_rand_normal (GRand *rand,
*/ */
void g_random_set_seed (guint32 seed); void g_random_set_seed (guint32 seed);
guint32 g_random_int (); guint32 g_random_int (void);
gint32 g_random_int_range (gint32 min, gint32 g_random_int_range (gint32 min,
gint32 max); gint32 max);
gdouble g_random_double (); gdouble g_random_double (void);
gdouble g_random_double_range (gdouble min, gdouble g_random_double_range (gdouble min,
gdouble max); gdouble max);
/* dito /* dito

View File

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

35
grand.c
View File

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

View File

@ -29,12 +29,27 @@ const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]);
int main() int main()
{ {
guint i; guint n;
GRand* rand = g_rand_new_with_seed (first_numbers[0]); GRand* rand = g_rand_new_with_seed (first_numbers[0]);
for (i = 1; i < length; i++) for (n = 1; n < length; n++)
g_assert (first_numbers[i]); g_assert (first_numbers[n] == g_rand_int (rand));
for (n = 1; n < 100000; n++)
{
gint32 i;
gdouble d;
i = g_rand_int_range (rand, 8,16);
g_assert (i >= 8 && i < 16);
d = g_rand_double (rand);
g_assert (d >= 0 && d < 1);
d = g_rand_double_range (rand, -8, 32);
g_assert (d >= -8 && d < 32);
}
g_rand_free (rand); g_rand_free (rand);