1 #undef G_DISABLE_ASSERT
6 /* Outputs tested against the reference implementation mt19937ar.c from
7 http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */
9 /* Tests for a simple seed, first number is the seed */
10 const guint32 first_numbers
[] =
36 const guint32 seed_array
[] =
44 /* tests for the array seed */
45 const guint32 array_outputs
[] =
59 const gint length
= sizeof (first_numbers
) / sizeof (first_numbers
[0]);
60 const gint seed_length
= sizeof (seed_array
) / sizeof (seed_array
[0]);
61 const gint array_length
= sizeof (array_outputs
) / sizeof (array_outputs
[0]);
69 GRand
* rand
= g_rand_new_with_seed (first_numbers
[0]);
72 for (n
= 1; n
< length
; n
++)
73 g_assert (first_numbers
[n
] == g_rand_int (rand
));
75 g_rand_set_seed (rand
, 2);
76 g_rand_set_seed_array (rand
, seed_array
, seed_length
);
78 for (n
= 0; n
< array_length
; n
++)
79 g_assert (array_outputs
[n
] == g_rand_int (rand
));
81 copy
= g_rand_copy (rand
);
82 for (n
= 0; n
< 100; n
++)
83 g_assert (g_rand_int (copy
) == g_rand_int (rand
));
85 for (n
= 1; n
< 100000; n
++)
91 i
= g_rand_int_range (rand
, 8,16);
92 g_assert (i
>= 8 && i
< 16);
94 i
= g_random_int_range (8,16);
95 g_assert (i
>= 8 && i
< 16);
97 d
= g_rand_double (rand
);
98 g_assert (d
>= 0 && d
< 1);
100 d
= g_random_double ();
101 g_assert (d
>= 0 && d
< 1);
103 d
= g_rand_double_range (rand
, -8, 32);
104 g_assert (d
>= -8 && d
< 32);
106 d
= g_random_double_range (-8, 32);
107 g_assert (d
>= -8 && d
< 32);
109 b
= g_random_boolean ();
110 g_assert (b
== TRUE
|| b
== FALSE
);
112 b
= g_rand_boolean (rand
);
113 g_assert (b
== TRUE
|| b
== FALSE
);
116 /* Statistical sanity check, count the number of ones
117 * when getting random numbers in range [0,3) and see
118 * that it must be semi-close to 0.25 with a VERY large
121 for (n
= 1; n
< 100000; n
++)
123 if (g_random_int_range (0, 4) == 1)
126 proportion
= (double)ones
/ (double)100000;
127 /* 0.025 is overkill, but should suffice to test for some unreasonability */
128 g_assert (ABS (proportion
- 0.25) < 0.025);