#ifdef G_OS_WIN32
#include <stdlib.h>
+#include <process.h> /* For getpid() */
#endif
/**
* generation, nonces, salts or one-time pads.
*
* This PRNG is suitable for non-cryptographic use such as in games
- * (shuffling a card deck, generating levels), generating data for a
- * test suite, etc. If you need random data for cryptographic
- * purposes, it is recommended to use platform-specific APIs such as
- * <literal>/dev/random</literal> on Unix, or CryptGenRandom() on
- * Windows.
+ * (shuffling a card deck, generating levels), generating data for
+ * a test suite, etc. If you need random data for cryptographic
+ * purposes, it is recommended to use platform-specific APIs such
+ * as `/dev/random` on UNIX, or CryptGenRandom() on Windows.
*
* GRand uses the Mersenne Twister PRNG, which was originally
* developed by Makoto Matsumoto and Takuji Nishimura. Further
- * information can be found at <ulink
- * url="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html">
- * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html</ulink>.
+ * information can be found at
+ * [this page](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html).
*
* If you just need a random number, you simply call the g_random_*
* functions, which will create a globally used #GRand and use the
*
* The g_rand*_range functions will return high quality equally
* distributed random numbers, whereas for example the
- * <literal>(g_random_int()%max)</literal> approach often
+ * `(g_random_int()%max)` approach often
* doesn't yield equally distributed numbers.
*
* GLib changed the seeding algorithm for the pseudo-random number
*
* The original seeding and generation algorithms, as found in
* GLib 2.0.x, can be used instead of the new ones by setting the
- * environment variable <envar>G_RANDOM_VERSION</envar> to the value
- * of '2.0'. Use the GLib-2.0 algorithms only if you have sequences
- * of numbers generated with Glib-2.0 that you need to reproduce
- * exactly.
+ * environment variable `G_RANDOM_VERSION` to the value of '2.0'.
+ * Use the GLib-2.0 algorithms only if you have sequences of numbers
+ * generated with Glib-2.0 that you need to reproduce exactly.
*/
/**
*
* Creates a new random number generator initialized with @seed.
*
- * Return value: the new #GRand
+ * Returns: the new #GRand
**/
GRand*
g_rand_new_with_seed (guint32 seed)
*
* Creates a new random number generator initialized with @seed.
*
- * Return value: the new #GRand
+ * Returns: the new #GRand
*
* Since: 2.4
*/
* g_rand_new:
*
* Creates a new random number generator initialized with a seed taken
- * either from <filename>/dev/urandom</filename> (if existing) or from
- * the current time (as a fallback).
+ * either from `/dev/urandom` (if existing) or from the current time
+ * (as a fallback).
*
* On Windows, the seed is taken from rand_s().
*
- * Return value: the new #GRand
+ * Returns: the new #GRand
*/
GRand*
g_rand_new (void)
seed[3] = getppid ();
}
#else /* G_OS_WIN32 */
+ /* rand_s() is only available since Visual Studio 2005 */
+#if defined(_MSC_VER) && _MSC_VER >= 1400
gint i;
for (i = 0; i < G_N_ELEMENTS (seed); i++)
rand_s (&seed[i]);
+#else
+#warning Using insecure seed for random number generation because of missing rand_s() in Windows XP
+ GTimeVal now;
+
+ g_get_current_time (&now);
+ seed[0] = now.tv_sec;
+ seed[1] = now.tv_usec;
+ seed[2] = getpid ();
+ seed[3] = 0;
+#endif
+
#endif
return g_rand_new_with_seed_array (seed, 4);
* This way you can take a snapshot of the random number generator for
* replaying later.
*
- * Return value: the new #GRand
+ * Returns: the new #GRand
*
* Since: 2.4
*/
* Returns the next random #guint32 from @rand_ equally distributed over
* the range [0..2^32-1].
*
- * Return value: a random number
+ * Returns: a random number
*/
guint32
g_rand_int (GRand *rand)
* Returns the next random #gint32 from @rand_ equally distributed over
* the range [@begin..@end-1].
*
- * Return value: a random number
+ * Returns: a random number
*/
gint32
g_rand_int_range (GRand *rand,
* Returns the next random #gdouble from @rand_ equally distributed over
* the range [0..1).
*
- * Return value: a random number
+ * Returns: a random number
*/
gdouble
g_rand_double (GRand *rand)
* Returns the next random #gdouble from @rand_ equally distributed over
* the range [@begin..@end).
*
- * Return value: a random number
+ * Returns: a random number
*/
gdouble
g_rand_double_range (GRand *rand,
* Return a random #guint32 equally distributed over the range
* [0..2^32-1].
*
- * Return value: a random number
+ * Returns: a random number
*/
guint32
g_random_int (void)
* Returns a random #gint32 equally distributed over the range
* [@begin..@end-1].
*
- * Return value: a random number
+ * Returns: a random number
*/
gint32
g_random_int_range (gint32 begin,
*
* Returns a random #gdouble equally distributed over the range [0..1).
*
- * Return value: a random number
+ * Returns: a random number
*/
gdouble
g_random_double (void)
* Returns a random #gdouble equally distributed over the range
* [@begin..@end).
*
- * Return value: a random number
+ * Returns: a random number
*/
gdouble
g_random_double_range (gdouble begin,