mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-07-19 00:17:51 +02:00
debian
docs
glib
gmodule
gobject
gthread
m4macros
po
tests
bookmarks
collate
gobject
markups
refcount
Makefile.am
array-test.c
asyncqueue-test.c
atomic-test.c
base64-test.c
bit-test.c
bookmarkfile-test.c
casefold.txt
casemap.txt
child-test.c
completion-test.c
convert-test.c
cxx-test.C
date-test.c
dirname-test.c
env-test.c
errorcheck-mutex-test.c
file-test.c
gen-casefold-txt.pl
gen-casemap-txt.pl
gio-test.c
hash-test.c
iochannel-test-infile
iochannel-test.c
keyfile-test.c
libmoduletestplugin_a.c
libmoduletestplugin_b.c
list-test.c
mainloop-test.c
makefile.msc.in
mapping-test.c
markup-escape-test.c
markup-test.c
memchunks.c
module-test.c
node-test.c
onceinit.c
option-test.c
patterntest.c
printf-test.c
qsort-test.c
queue-test.c
rand-test.c
regex-test.c
relation-test.c
run-bookmark-test.sh
run-collate-tests.sh
run-markup-tests.sh
sequence-test.c
shell-test.c
slice-color.c
slice-concurrent.c
slice-test.c
slice-threadinit.c
slist-test.c
spawn-test-win32-gui.c
spawn-test.c
strfunc-test.c
string-test.c
strtod-test.c
strtoll-test.c
testgdate.c
testgdateparser.c
testglib.c
thread-test.c
threadpool-test.c
timeloop-basic.c
timeloop-closure.c
timeloop.c
tree-test.c
type-test.c
unicode-caseconv.c
unicode-collate.c
unicode-encoding.c
unicode-normalize.c
uri-test.c
utf8-pointer.c
utf8-validate.c
utf8.txt
AUTHORS
COPYING
ChangeLog
ChangeLog.pre-1-2
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-14
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL
INSTALL.in
MAINTAINERS
Makefile.am
NEWS
NEWS.pre-1-3
README
README.in
README.win32
acglib.m4
acinclude.m4
autogen.sh
config.h.win32.in
configure.in
glib-2.0-uninstalled.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-zip.in
glibconfig.h.win32.in
gmodule-2.0-uninstalled.pc.in
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0-uninstalled.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0-uninstalled.pc.in
gobject-2.0.pc.in
gthread-2.0-uninstalled.pc.in
gthread-2.0.pc.in
makefile.msc
mkinstalldirs
msvc_recommended_pragmas.h
sanity_check
win32-fixup.pl
Fri Dec 19 11:49:21 2003 George Lebl <jirka@5z.com> * glib/grand.c glib/grand.h (g_rand_new) (g_rand_new_with_seed) (g_rand_new_with_seed_array) (g_rand_set_seed_array): Add the init_by_array functionality from the reference implementation of the mersenne twister (mt19937ar.c) and change the naming to fit with the rest of the grand API. New functions are g_rand_new_with_seed_array, g_rand_set_seed_array. This is only reliable/tested for the 2.2 version of the seeding as that's what the reference implementation uses. Also modify g_rand_new to get 4 longs from /dev/urandom since that will always be available anyway and we get more entropy and if /dev/urandom is unavailable use also 4 longs for seeding using secs, usecs, getpid and getppid. For version 2.0 use only a simple seed again but be more careful about seeding with secs/usecs in this case. * glib/grand.c glib/grand.h (g_rand_copy): Add g_rand_copy function to copy the current state of the random number generator. * glib/grand.c (g_rand_new): Add testing for EINTR when reading from /dev/urandom * tests/rand-test.c: add testing of the array seeding stuff against the reference implementation, plus add statistical sanity check to see that the values outputted are truly kind of random. And check that g_rand_copy truly copies the state by checking a few terms.
136 lines
2.8 KiB
C
136 lines
2.8 KiB
C
#undef G_DISABLE_ASSERT
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#include <glib.h>
|
|
|
|
/* Outputs tested against the reference implementation mt19937ar.c from
|
|
http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */
|
|
|
|
/* Tests for a simple seed, first number is the seed */
|
|
const guint32 first_numbers[] =
|
|
{
|
|
0x7a7a7a7a,
|
|
0xfdcc2d54,
|
|
0x3a279ceb,
|
|
0xc4d39c33,
|
|
0xf31895cd,
|
|
0x46ca0afc,
|
|
0x3f5484ff,
|
|
0x54bc9557,
|
|
0xed2c24b1,
|
|
0x84062503,
|
|
0x8f6404b3,
|
|
0x599a94b3,
|
|
0xe46d03d5,
|
|
0x310beb78,
|
|
0x7bee5d08,
|
|
0x760d09be,
|
|
0x59b6e163,
|
|
0xbf6d16ec,
|
|
0xcca5fb54,
|
|
0x5de7259b,
|
|
0x1696330c,
|
|
};
|
|
|
|
/* array seed */
|
|
const guint32 seed_array[] =
|
|
{
|
|
0x6553375f,
|
|
0xd6b8d43b,
|
|
0xa1e7667f,
|
|
0x2b10117c
|
|
};
|
|
|
|
/* tests for the array seed */
|
|
const guint32 array_outputs[] =
|
|
{
|
|
0xc22b7dc3,
|
|
0xfdecb8ae,
|
|
0xb4af0738,
|
|
0x516bc6e1,
|
|
0x7e372e91,
|
|
0x2d38ff80,
|
|
0x6096494a,
|
|
0xd162d5a8,
|
|
0x3c0aaa0d,
|
|
0x10e736ae
|
|
};
|
|
|
|
const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]);
|
|
const gint seed_length = sizeof (seed_array) / sizeof (seed_array[0]);
|
|
const gint array_length = sizeof (array_outputs) / sizeof (array_outputs[0]);
|
|
|
|
int main()
|
|
{
|
|
guint n;
|
|
guint ones;
|
|
double proportion;
|
|
|
|
GRand* rand = g_rand_new_with_seed (first_numbers[0]);
|
|
GRand* copy;
|
|
|
|
for (n = 1; n < length; n++)
|
|
g_assert (first_numbers[n] == g_rand_int (rand));
|
|
|
|
g_rand_set_seed (rand, 2);
|
|
g_rand_set_seed_array (rand, seed_array, seed_length);
|
|
|
|
for (n = 0; n < array_length; n++)
|
|
g_assert (array_outputs[n] == g_rand_int (rand));
|
|
|
|
copy = g_rand_copy (rand);
|
|
for (n = 0; n < 100; n++)
|
|
g_assert (g_rand_int (copy) == g_rand_int (rand));
|
|
|
|
for (n = 1; n < 100000; n++)
|
|
{
|
|
gint32 i;
|
|
gdouble d;
|
|
gboolean b;
|
|
|
|
i = g_rand_int_range (rand, 8,16);
|
|
g_assert (i >= 8 && i < 16);
|
|
|
|
i = g_random_int_range (8,16);
|
|
g_assert (i >= 8 && i < 16);
|
|
|
|
d = g_rand_double (rand);
|
|
g_assert (d >= 0 && d < 1);
|
|
|
|
d = g_random_double ();
|
|
g_assert (d >= 0 && d < 1);
|
|
|
|
d = g_rand_double_range (rand, -8, 32);
|
|
g_assert (d >= -8 && d < 32);
|
|
|
|
d = g_random_double_range (-8, 32);
|
|
g_assert (d >= -8 && d < 32);
|
|
|
|
b = g_random_boolean ();
|
|
g_assert (b == TRUE || b == FALSE);
|
|
|
|
b = g_rand_boolean (rand);
|
|
g_assert (b == TRUE || b == FALSE);
|
|
}
|
|
|
|
/* Statistical sanity check, count the number of ones
|
|
* when getting random numbers in range [0,3) and see
|
|
* that it must be semi-close to 0.25 with a VERY large
|
|
* probability */
|
|
ones = 0;
|
|
for (n = 1; n < 100000; n++)
|
|
{
|
|
if (g_random_int_range (0, 4) == 1)
|
|
ones ++;
|
|
}
|
|
proportion = (double)ones / (double)100000;
|
|
/* 0.025 is overkill, but should suffice to test for some unreasonability */
|
|
g_assert (ABS (proportion - 0.25) < 0.025);
|
|
|
|
g_rand_free (rand);
|
|
g_rand_free (copy);
|
|
|
|
return 0;
|
|
}
|
|
|