Improved the seeding algorithm. Old behaviour can be achived by setting

2002-11-26  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>

	* glib/grand.c, gthread/gthread-impl.c, tests/rand-test.c:
	Improved the seeding algorithm. Old behaviour can be achived by
	setting envvar G_RANDOM_VERSION to "2.0". (#99262)

	* docs/reference/glib/glib-docs.sgml,
	docs/reference/glib/Makefile.am: Renamed
	docs/reference/glib/changes-2.0.sgml to
	docs/reference/glib/changes.sgml and added section for changes
	from 2.0 to 2.2 (Also corrected 1.0 to 1.2).

	* README.in, docs/reference/glib/running.sgml,
	docs/reference/glib/tmpl/random_numbers.sgml,
	docs/reference/glib/changes.sgml: Added notes about the new
	seeding algorithm.
This commit is contained in:
Sebastian Wilhelmi 2002-11-26 14:09:00 +00:00 committed by Sebastian Wilhelmi
parent 17344bb1e4
commit c838b2a071
16 changed files with 281 additions and 50 deletions

View File

@ -1,9 +1,24 @@
2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib/grand.c, gthread/gthread-impl.c, tests/rand-test.c:
Changed the seeding algorithm. Old behaviour can be achived by
setting envvar G_RANDOM_VERSION to "2.0". (#99262)
* docs/reference/glib/glib-docs.sgml,
docs/reference/glib/Makefile.am: Renamed
docs/reference/glib/changes-2.0.sgml to
docs/reference/glib/changes.sgml and added section for changes
from 2.0 to 2.2 (Also corrected 1.0 to 1.2).
* README.in, docs/reference/glib/running.sgml,
docs/reference/glib/tmpl/random_numbers.sgml,
docs/reference/glib/changes.sgml: Added notes about the new
seeding algorithm.
* configure.in: Make CPPFLAGS, not CFLAGS, include * configure.in: Make CPPFLAGS, not CFLAGS, include
G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib
programs would link to the threads library on some platforms. Also programs would link to the threads library on some platforms. Also
fixed a bug manifesting through this change. fixed a bug manifesting through this change. (#77981)
2002-11-26 Matthias Clasen <maclas@gmx.de> 2002-11-26 Matthias Clasen <maclas@gmx.de>

View File

@ -1,9 +1,24 @@
2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib/grand.c, gthread/gthread-impl.c, tests/rand-test.c:
Changed the seeding algorithm. Old behaviour can be achived by
setting envvar G_RANDOM_VERSION to "2.0". (#99262)
* docs/reference/glib/glib-docs.sgml,
docs/reference/glib/Makefile.am: Renamed
docs/reference/glib/changes-2.0.sgml to
docs/reference/glib/changes.sgml and added section for changes
from 2.0 to 2.2 (Also corrected 1.0 to 1.2).
* README.in, docs/reference/glib/running.sgml,
docs/reference/glib/tmpl/random_numbers.sgml,
docs/reference/glib/changes.sgml: Added notes about the new
seeding algorithm.
* configure.in: Make CPPFLAGS, not CFLAGS, include * configure.in: Make CPPFLAGS, not CFLAGS, include
G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib
programs would link to the threads library on some platforms. Also programs would link to the threads library on some platforms. Also
fixed a bug manifesting through this change. fixed a bug manifesting through this change. (#77981)
2002-11-26 Matthias Clasen <maclas@gmx.de> 2002-11-26 Matthias Clasen <maclas@gmx.de>

View File

@ -1,9 +1,24 @@
2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib/grand.c, gthread/gthread-impl.c, tests/rand-test.c:
Changed the seeding algorithm. Old behaviour can be achived by
setting envvar G_RANDOM_VERSION to "2.0". (#99262)
* docs/reference/glib/glib-docs.sgml,
docs/reference/glib/Makefile.am: Renamed
docs/reference/glib/changes-2.0.sgml to
docs/reference/glib/changes.sgml and added section for changes
from 2.0 to 2.2 (Also corrected 1.0 to 1.2).
* README.in, docs/reference/glib/running.sgml,
docs/reference/glib/tmpl/random_numbers.sgml,
docs/reference/glib/changes.sgml: Added notes about the new
seeding algorithm.
* configure.in: Make CPPFLAGS, not CFLAGS, include * configure.in: Make CPPFLAGS, not CFLAGS, include
G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib
programs would link to the threads library on some platforms. Also programs would link to the threads library on some platforms. Also
fixed a bug manifesting through this change. fixed a bug manifesting through this change. (#77981)
2002-11-26 Matthias Clasen <maclas@gmx.de> 2002-11-26 Matthias Clasen <maclas@gmx.de>

View File

@ -1,9 +1,24 @@
2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib/grand.c, gthread/gthread-impl.c, tests/rand-test.c:
Changed the seeding algorithm. Old behaviour can be achived by
setting envvar G_RANDOM_VERSION to "2.0". (#99262)
* docs/reference/glib/glib-docs.sgml,
docs/reference/glib/Makefile.am: Renamed
docs/reference/glib/changes-2.0.sgml to
docs/reference/glib/changes.sgml and added section for changes
from 2.0 to 2.2 (Also corrected 1.0 to 1.2).
* README.in, docs/reference/glib/running.sgml,
docs/reference/glib/tmpl/random_numbers.sgml,
docs/reference/glib/changes.sgml: Added notes about the new
seeding algorithm.
* configure.in: Make CPPFLAGS, not CFLAGS, include * configure.in: Make CPPFLAGS, not CFLAGS, include
G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib
programs would link to the threads library on some platforms. Also programs would link to the threads library on some platforms. Also
fixed a bug manifesting through this change. fixed a bug manifesting through this change. (#77981)
2002-11-26 Matthias Clasen <maclas@gmx.de> 2002-11-26 Matthias Clasen <maclas@gmx.de>

View File

@ -1,9 +1,24 @@
2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib/grand.c, gthread/gthread-impl.c, tests/rand-test.c:
Changed the seeding algorithm. Old behaviour can be achived by
setting envvar G_RANDOM_VERSION to "2.0". (#99262)
* docs/reference/glib/glib-docs.sgml,
docs/reference/glib/Makefile.am: Renamed
docs/reference/glib/changes-2.0.sgml to
docs/reference/glib/changes.sgml and added section for changes
from 2.0 to 2.2 (Also corrected 1.0 to 1.2).
* README.in, docs/reference/glib/running.sgml,
docs/reference/glib/tmpl/random_numbers.sgml,
docs/reference/glib/changes.sgml: Added notes about the new
seeding algorithm.
* configure.in: Make CPPFLAGS, not CFLAGS, include * configure.in: Make CPPFLAGS, not CFLAGS, include
G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib
programs would link to the threads library on some platforms. Also programs would link to the threads library on some platforms. Also
fixed a bug manifesting through this change. fixed a bug manifesting through this change. (#77981)
2002-11-26 Matthias Clasen <maclas@gmx.de> 2002-11-26 Matthias Clasen <maclas@gmx.de>

View File

@ -1,9 +1,24 @@
2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib/grand.c, gthread/gthread-impl.c, tests/rand-test.c:
Changed the seeding algorithm. Old behaviour can be achived by
setting envvar G_RANDOM_VERSION to "2.0". (#99262)
* docs/reference/glib/glib-docs.sgml,
docs/reference/glib/Makefile.am: Renamed
docs/reference/glib/changes-2.0.sgml to
docs/reference/glib/changes.sgml and added section for changes
from 2.0 to 2.2 (Also corrected 1.0 to 1.2).
* README.in, docs/reference/glib/running.sgml,
docs/reference/glib/tmpl/random_numbers.sgml,
docs/reference/glib/changes.sgml: Added notes about the new
seeding algorithm.
* configure.in: Make CPPFLAGS, not CFLAGS, include * configure.in: Make CPPFLAGS, not CFLAGS, include
G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib
programs would link to the threads library on some platforms. Also programs would link to the threads library on some platforms. Also
fixed a bug manifesting through this change. fixed a bug manifesting through this change. (#77981)
2002-11-26 Matthias Clasen <maclas@gmx.de> 2002-11-26 Matthias Clasen <maclas@gmx.de>

View File

@ -1,9 +1,24 @@
2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de> 2002-11-26 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib/grand.c, gthread/gthread-impl.c, tests/rand-test.c:
Changed the seeding algorithm. Old behaviour can be achived by
setting envvar G_RANDOM_VERSION to "2.0". (#99262)
* docs/reference/glib/glib-docs.sgml,
docs/reference/glib/Makefile.am: Renamed
docs/reference/glib/changes-2.0.sgml to
docs/reference/glib/changes.sgml and added section for changes
from 2.0 to 2.2 (Also corrected 1.0 to 1.2).
* README.in, docs/reference/glib/running.sgml,
docs/reference/glib/tmpl/random_numbers.sgml,
docs/reference/glib/changes.sgml: Added notes about the new
seeding algorithm.
* configure.in: Make CPPFLAGS, not CFLAGS, include * configure.in: Make CPPFLAGS, not CFLAGS, include
G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib G_THREAD_CFLAGS. CFLAGS is used while linking too and thus GLib
programs would link to the threads library on some platforms. Also programs would link to the threads library on some platforms. Also
fixed a bug manifesting through this change. fixed a bug manifesting through this change. (#77981)
2002-11-26 Matthias Clasen <maclas@gmx.de> 2002-11-26 Matthias Clasen <maclas@gmx.de>

View File

@ -24,6 +24,22 @@ Installation
See the file 'INSTALL' See the file 'INSTALL'
Notes about GLib-2.2.0
======================
* GLib changed the seeding algorithm for the pseudo-random number
generator Mersenne Twister, as used by GRand and GRandom. This was
necessary, because some seeds would yield very bad pseudo-random
streams. Further information can be found at:
http://www.math.keio.ac.jp/~matumoto/emt.html
The original seeding algorithm, as found in GLib-2.0.x, can be used
instead of the new one by setting the environment variable
G_RANDOM_VERSION to the value of "2.0". Use the GLib-2.0 algorithm
only if you have sequences of numbers generated with Glib-2.0 that
you need to reproduce exactly.
How to report bugs How to report bugs
================== ==================

View File

@ -48,7 +48,7 @@ HTML_IMAGES = \
content_files = \ content_files = \
running.sgml \ running.sgml \
building.sgml \ building.sgml \
changes-2.0.sgml \ changes.sgml \
compiling.sgml \ compiling.sgml \
resources.sgml \ resources.sgml \
version.xml version.xml

View File

@ -1,26 +1,58 @@
<refentry id="glib-changes-2-0" revision="17 Jan 2002"> <refentry id="glib-changes" revision="17 Jan 2002">
<refmeta> <refmeta>
<refentrytitle>Changes from 1.0 to 2.0</refentrytitle> <refentrytitle>Changes to GLib</refentrytitle>
<manvolnum>3</manvolnum> <manvolnum>3</manvolnum>
<refmiscinfo>Changes from 1.0 to 2.0</refmiscinfo> <refmiscinfo>Changes to GLib</refmiscinfo>
</refmeta> </refmeta>
<refnamediv> <refnamediv>
<refname>Changes from 1.0 to 2.0</refname> <refname>Changes to GLib</refname>
<refpurpose> <refpurpose>
Incompatible changes made between version 1.0 and version 2.0 Incompatible changes made between successing versions of GLib
</refpurpose> </refpurpose>
</refnamediv> </refnamediv>
<refsect1> <refsect1>
<title>Incompatible changes from 1.0 to 2.0</title> <title>Incompatible changes from 2.0 to 2.2</title>
<itemizedlist>
<listitem>
<para>
GLib changed the seeding algorithm for the pseudo-random number
generator Mersenne Twister, as used by <structname>GRand</structname>
and <structname>GRandom</structname>. This was necessary, because some
seeds would yield very bad pseudo-random streams.
</para>
<para>
Further information can be found at the website of the Mersenne
Twister random number generator at <ulink
url="http://www.math.keio.ac.jp/~matumoto/emt.html">http://www.math.keio.ac.jp/~matumoto/emt.html</ulink>.
</para>
<para>
The original seeding algorithm, as found in GLib 2.0.x, can be used
instead of the new one by setting the environment variable
<envar>G_RANDOM_VERSION</envar> to the value of '2.0'. Use the
GLib-2.0 algorithm only if you have sequences of numbers generated
with Glib-2.0 that you need to reproduce exactly.
</para>
</listitem>
</itemizedlist>
</refsect1>
<refsect1>
<title>Incompatible changes from 1.2 to 2.0</title>
<para> <para>
The <ulink url="http://developer.gnome.org/dotplan/porting/">GNOME 2.0 The <ulink url="http://developer.gnome.org/dotplan/porting/">GNOME 2.0
porting guide</ulink> on <ulink porting guide</ulink> on <ulink
url="http://developer.gnome.org">http://developer.gnome.org</ulink> url="http://developer.gnome.org">http://developer.gnome.org</ulink>
has some more detailed discussion of porting from 1.0 to 2.0. has some more detailed discussion of porting from 1.2 to 2.0.
See the section on GLib. See the section on GLib.
</para> </para>

View File

@ -58,7 +58,7 @@
<!ENTITY glib-Building SYSTEM "building.sgml"> <!ENTITY glib-Building SYSTEM "building.sgml">
<!ENTITY glib-Running SYSTEM "running.sgml"> <!ENTITY glib-Running SYSTEM "running.sgml">
<!ENTITY glib-Resources SYSTEM "resources.sgml"> <!ENTITY glib-Resources SYSTEM "resources.sgml">
<!ENTITY glib-Changes-2-0 SYSTEM "changes-2.0.sgml"> <!ENTITY glib-Changes SYSTEM "changes.sgml">
<!ENTITY version SYSTEM "version.xml"> <!ENTITY version SYSTEM "version.xml">
]> ]>
@ -107,7 +107,7 @@ implementations, e.g. POSIX threads, DCE threads or Solaris threads.
&glib-Building; &glib-Building;
&glib-Compiling; &glib-Compiling;
&glib-Running; &glib-Running;
&glib-Changes-2-0; &glib-Changes;
&glib-Resources; &glib-Resources;
</chapter> </chapter>

View File

@ -62,6 +62,17 @@ variables like <envar>LANG</envar>, <envar>PATH</envar> or <envar>HOME</envar>.
</para> </para>
</formalpara> </formalpara>
<formalpara>
<title><envar>G_RANDOM_VERSION</envar></title>
<para>
If this environment variable is set to '2.0', the outdated
pseudo-random number seeding algorithm from GLib-2.0 is used
instead of the new better one. Use the GLib-2.0 algorithm only if
you have sequences of numbers generated with Glib-2.0 that you
need to reproduce exactly.
</para>
</formalpara>
</refsect2> </refsect2>
<refsect2> <refsect2>

View File

@ -2,12 +2,12 @@
Random Numbers Random Numbers
<!-- ##### SECTION Short_Description ##### --> <!-- ##### SECTION Short_Description ##### -->
pseudo random number generator. pseudo-random number generator.
<!-- ##### SECTION Long_Description ##### --> <!-- ##### SECTION Long_Description ##### -->
<para> <para>
The following functions allow you to use a portable, fast and good The following functions allow you to use a portable, fast and good
pseudo random number generator (PRNG). It uses the Mersenne Twister pseudo-random number generator (PRNG). It uses the Mersenne Twister
PRNG, which was originally developed by Makoto Matsumoto and Takuji PRNG, which was originally developed by Makoto Matsumoto and Takuji
Nishimura. Further information can be found at <ulink Nishimura. Further information can be found at <ulink
url="http://www.math.keio.ac.jp/~matumoto/emt.html" url="http://www.math.keio.ac.jp/~matumoto/emt.html"
@ -32,6 +32,18 @@ equally distributed random numbers, whereas for example the
yield equally distributed numbers. yield equally distributed numbers.
</para> </para>
<para>
GLib changed the seeding algorithm for the pseudo-random number
generator Mersenne Twister, as used by <structname>GRand</structname>
and <structname>GRandom</structname>. This was necessary, because some
seeds would yield very bad pseudo-random streams. The original seeding
algorithm, as found in GLib 2.0.x, can be used instead of the new one
by setting the environment variable <envar>G_RANDOM_VERSION</envar> to
the value of '2.0'. Use the GLib-2.0 algorithm only if you have
sequences of numbers generated with Glib-2.0 that you need to
reproduce exactly.
</para>
<!-- ##### SECTION See_Also ##### --> <!-- ##### SECTION See_Also ##### -->
<para> <para>

View File

@ -58,6 +58,41 @@ static GRand* global_random = NULL;
#define TEMPERING_SHIFT_T(y) (y << 15) #define TEMPERING_SHIFT_T(y) (y << 15)
#define TEMPERING_SHIFT_L(y) (y >> 18) #define TEMPERING_SHIFT_L(y) (y >> 18)
static guint
get_random_version (void)
{
static gboolean initialized = FALSE;
static guint random_version;
if (!initialized)
{
const gchar *version_string = g_getenv ("G_RANDOM_VERSION");
if (!version_string || version_string[0] == '\000' ||
strcmp (version_string, "2.2") == 0)
random_version = 22;
else if (strcmp (version_string, "2.0") == 0)
random_version = 20;
else
{
g_warning ("Unknown G_RANDOM_VERSION \"%s\". Using version 2.2.",
version_string);
random_version = 22;
}
initialized = TRUE;
}
return random_version;
}
/* This is called from g_thread_init(). It's used to
* initialize some static data in a threadsafe way.
*/
void
g_rand_init (void)
{
(void)get_random_version ();
}
struct _GRand struct _GRand
{ {
guint32 mt[N]; /* the array for the state vector */ guint32 mt[N]; /* the array for the state vector */
@ -148,6 +183,9 @@ g_rand_set_seed (GRand* rand, guint32 seed)
{ {
g_return_if_fail (rand != NULL); g_return_if_fail (rand != NULL);
switch (get_random_version ())
{
case 20:
/* 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 */
/* [KNUTH 1981, The Art of Computer Programming */ /* [KNUTH 1981, The Art of Computer Programming */
@ -156,9 +194,24 @@ g_rand_set_seed (GRand* rand, guint32 seed)
if (seed == 0) /* This would make the PRNG procude only zeros */ if (seed == 0) /* This would make the PRNG procude only zeros */
seed = 0x6b842128; /* Just set it to another number */ seed = 0x6b842128; /* Just set it to another number */
rand->mt[0]= seed & 0xffffffff; rand->mt[0]= seed;
for (rand->mti=1; rand->mti<N; rand->mti++) for (rand->mti=1; rand->mti<N; rand->mti++)
rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]) & 0xffffffff; rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]);
break;
case 22:
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous version (see above), MSBs of the */
/* seed affect only MSBs of the array mt[]. */
rand->mt[0]= seed;
for (rand->mti=1; rand->mti<N; rand->mti++)
rand->mt[rand->mti] = 1812433253UL *
(rand->mt[rand->mti-1] ^ (rand->mt[rand->mti-1] >> 30)) + rand->mti;
break;
default:
g_assert_not_reached ();
}
} }
/** /**

View File

@ -66,6 +66,7 @@ void g_mutex_init (void);
void g_mem_init (void); void g_mem_init (void);
void g_messages_init (void); void g_messages_init (void);
void g_convert_init (void); void g_convert_init (void);
void g_rand_init (void);
void g_main_thread_init (void); void g_main_thread_init (void);
#define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE))) #define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE)))
@ -382,6 +383,7 @@ g_thread_init (GThreadFunctions* init)
g_mem_init (); g_mem_init ();
g_messages_init (); g_messages_init ();
g_convert_init (); g_convert_init ();
g_rand_init ();
g_main_thread_init (); g_main_thread_init ();
/* now we can set g_threads_got_initialized and thus enable /* now we can set g_threads_got_initialized and thus enable

View File

@ -6,26 +6,26 @@
const gint32 first_numbers[] = const gint32 first_numbers[] =
{ {
0x7a7a7a7a, 0x7a7a7a7a,
0x20aea82a, 0xfdcc2d54,
0xcab337ab, 0x3a279ceb,
0xdcf770ea, 0xc4d39c33,
0xdf552b2f, 0xf31895cd,
0x32d1ef7f, 0x46ca0afc,
0x6bed6dd9, 0x3f5484ff,
0x7222df44, 0x54bc9557,
0x6b842128, 0xed2c24b1,
0x07f8579a, 0x84062503,
0x9dad1004, 0x8f6404b3,
0x2df264f2, 0x599a94b3,
0x13b48989, 0xe46d03d5,
0xf2929475, 0x310beb78,
0x30f30c97, 0x7bee5d08,
0x3f9a1ea7, 0x760d09be,
0x3bf04710, 0x59b6e163,
0xb85bd69e, 0xbf6d16ec,
0x790a48b0, 0xcca5fb54,
0xfa06b85f, 0x5de7259b,
0xa64cc9e3 0x1696330c,
}; };
const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]); const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]);