Blame glib/grand.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* Originally developed and coded by Makoto Matsumoto and Takuji
Packit ae235b
 * Nishimura.  Please mail <matumoto@math.keio.ac.jp>, if you're using
Packit ae235b
 * code from this file in your own programs or libraries.
Packit ae235b
 * Further information on the Mersenne Twister can be found at
Packit ae235b
 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
Packit ae235b
 * This code was adapted to glib by Sebastian Wilhelmi.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#define _CRT_RAND_S
Packit ae235b
Packit ae235b
#include <math.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include "grand.h"
Packit ae235b
Packit ae235b
#include "genviron.h"
Packit ae235b
#include "gmain.h"
Packit ae235b
#include "gmem.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <process.h> /* For getpid() */
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:random_numbers
Packit ae235b
 * @title: Random Numbers
Packit ae235b
 * @short_description: pseudo-random number generator
Packit ae235b
 *
Packit ae235b
 * The following functions allow you to use a portable, fast and good
Packit ae235b
 * pseudo-random number generator (PRNG).
Packit ae235b
 * 
Packit ae235b
 * Do not use this API for cryptographic purposes such as key
Packit ae235b
 * generation, nonces, salts or one-time pads.
Packit ae235b
 *
Packit ae235b
 * This PRNG is suitable for non-cryptographic use such as in games
Packit ae235b
 * (shuffling a card deck, generating levels), generating data for
Packit ae235b
 * a test suite, etc. If you need random data for cryptographic
Packit ae235b
 * purposes, it is recommended to use platform-specific APIs such
Packit ae235b
 * as `/dev/random` on UNIX, or CryptGenRandom() on Windows.
Packit ae235b
 *
Packit ae235b
 * GRand uses the Mersenne Twister PRNG, which was originally
Packit ae235b
 * developed by Makoto Matsumoto and Takuji Nishimura. Further
Packit ae235b
 * information can be found at
Packit ae235b
 * [this page](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html).
Packit ae235b
 *
Packit ae235b
 * If you just need a random number, you simply call the g_random_*
Packit ae235b
 * functions, which will create a globally used #GRand and use the
Packit ae235b
 * according g_rand_* functions internally. Whenever you need a
Packit ae235b
 * stream of reproducible random numbers, you better create a
Packit ae235b
 * #GRand yourself and use the g_rand_* functions directly, which
Packit ae235b
 * will also be slightly faster. Initializing a #GRand with a
Packit ae235b
 * certain seed will produce exactly the same series of random
Packit ae235b
 * numbers on all platforms. This can thus be used as a seed for
Packit ae235b
 * e.g. games.
Packit ae235b
 *
Packit ae235b
 * The g_rand*_range functions will return high quality equally
Packit ae235b
 * distributed random numbers, whereas for example the
Packit ae235b
 * `(g_random_int()%max)` approach often
Packit ae235b
 * doesn't yield equally distributed numbers.
Packit ae235b
 *
Packit ae235b
 * GLib changed the seeding algorithm for the pseudo-random number
Packit ae235b
 * generator Mersenne Twister, as used by #GRand. This was necessary,
Packit ae235b
 * because some seeds would yield very bad pseudo-random streams.
Packit ae235b
 * Also the pseudo-random integers generated by g_rand*_int_range()
Packit ae235b
 * will have a slightly better equal distribution with the new
Packit ae235b
 * version of GLib.
Packit ae235b
 *
Packit ae235b
 * The original seeding and generation algorithms, as found in
Packit ae235b
 * GLib 2.0.x, can be used instead of the new ones by setting the
Packit ae235b
 * environment variable `G_RANDOM_VERSION` to the value of '2.0'.
Packit ae235b
 * Use the GLib-2.0 algorithms only if you have sequences of numbers
Packit ae235b
 * generated with Glib-2.0 that you need to reproduce exactly.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GRand:
Packit ae235b
 *
Packit ae235b
 * The GRand struct is an opaque data structure. It should only be
Packit ae235b
 * accessed through the g_rand_* functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (global_random);
Packit ae235b
Packit ae235b
/* Period parameters */  
Packit ae235b
#define N 624
Packit ae235b
#define M 397
Packit ae235b
#define MATRIX_A 0x9908b0df   /* constant vector a */
Packit ae235b
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
Packit ae235b
#define LOWER_MASK 0x7fffffff /* least significant r bits */
Packit ae235b
Packit ae235b
/* Tempering parameters */   
Packit ae235b
#define TEMPERING_MASK_B 0x9d2c5680
Packit ae235b
#define TEMPERING_MASK_C 0xefc60000
Packit ae235b
#define TEMPERING_SHIFT_U(y)  (y >> 11)
Packit ae235b
#define TEMPERING_SHIFT_S(y)  (y << 7)
Packit ae235b
#define TEMPERING_SHIFT_T(y)  (y << 15)
Packit ae235b
#define TEMPERING_SHIFT_L(y)  (y >> 18)
Packit ae235b
Packit ae235b
static guint
Packit ae235b
get_random_version (void)
Packit ae235b
{
Packit ae235b
  static gsize initialized = FALSE;
Packit ae235b
  static guint random_version;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&initialized))
Packit ae235b
    {
Packit ae235b
      const gchar *version_string = g_getenv ("G_RANDOM_VERSION");
Packit ae235b
      if (!version_string || version_string[0] == '\000' || 
Packit ae235b
	  strcmp (version_string, "2.2") == 0)
Packit ae235b
	random_version = 22;
Packit ae235b
      else if (strcmp (version_string, "2.0") == 0)
Packit ae235b
	random_version = 20;
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  g_warning ("Unknown G_RANDOM_VERSION \"%s\". Using version 2.2.",
Packit ae235b
		     version_string);
Packit ae235b
	  random_version = 22;
Packit ae235b
	}
Packit ae235b
      g_once_init_leave (&initialized, TRUE);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return random_version;
Packit ae235b
}
Packit ae235b
Packit ae235b
struct _GRand
Packit ae235b
{
Packit ae235b
  guint32 mt[N]; /* the array for the state vector  */
Packit ae235b
  guint mti; 
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_new_with_seed:
Packit ae235b
 * @seed: a value to initialize the random number generator
Packit ae235b
 * 
Packit ae235b
 * Creates a new random number generator initialized with @seed.
Packit ae235b
 * 
Packit ae235b
 * Returns: the new #GRand
Packit ae235b
 **/
Packit ae235b
GRand*
Packit ae235b
g_rand_new_with_seed (guint32 seed)
Packit ae235b
{
Packit ae235b
  GRand *rand = g_new0 (GRand, 1);
Packit ae235b
  g_rand_set_seed (rand, seed);
Packit ae235b
  return rand;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_new_with_seed_array:
Packit ae235b
 * @seed: an array of seeds to initialize the random number generator
Packit ae235b
 * @seed_length: an array of seeds to initialize the random number
Packit ae235b
 *     generator
Packit ae235b
 * 
Packit ae235b
 * Creates a new random number generator initialized with @seed.
Packit ae235b
 * 
Packit ae235b
 * Returns: the new #GRand
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
GRand*
Packit ae235b
g_rand_new_with_seed_array (const guint32 *seed,
Packit ae235b
                            guint          seed_length)
Packit ae235b
{
Packit ae235b
  GRand *rand = g_new0 (GRand, 1);
Packit ae235b
  g_rand_set_seed_array (rand, seed, seed_length);
Packit ae235b
  return rand;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_new:
Packit ae235b
 * 
Packit ae235b
 * Creates a new random number generator initialized with a seed taken
Packit ae235b
 * either from `/dev/urandom` (if existing) or from the current time
Packit ae235b
 * (as a fallback).
Packit ae235b
 *
Packit ae235b
 * On Windows, the seed is taken from rand_s().
Packit ae235b
 * 
Packit ae235b
 * Returns: the new #GRand
Packit ae235b
 */
Packit ae235b
GRand* 
Packit ae235b
g_rand_new (void)
Packit ae235b
{
Packit ae235b
  guint32 seed[4];
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  static gboolean dev_urandom_exists = TRUE;
Packit ae235b
  GTimeVal now;
Packit ae235b
Packit ae235b
  if (dev_urandom_exists)
Packit ae235b
    {
Packit ae235b
      FILE* dev_urandom;
Packit ae235b
Packit ae235b
      do
Packit ae235b
	{
Packit ae235b
	  dev_urandom = fopen("/dev/urandom", "rb");
Packit ae235b
	}
Packit ae235b
      while G_UNLIKELY (dev_urandom == NULL && errno == EINTR);
Packit ae235b
Packit ae235b
      if (dev_urandom)
Packit ae235b
	{
Packit ae235b
	  int r;
Packit ae235b
Packit ae235b
	  setvbuf (dev_urandom, NULL, _IONBF, 0);
Packit ae235b
	  do
Packit ae235b
	    {
Packit ae235b
	      errno = 0;
Packit ae235b
	      r = fread (seed, sizeof (seed), 1, dev_urandom);
Packit ae235b
	    }
Packit ae235b
	  while G_UNLIKELY (errno == EINTR);
Packit ae235b
Packit ae235b
	  if (r != 1)
Packit ae235b
	    dev_urandom_exists = FALSE;
Packit ae235b
Packit ae235b
	  fclose (dev_urandom);
Packit ae235b
	}	
Packit ae235b
      else
Packit ae235b
	dev_urandom_exists = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!dev_urandom_exists)
Packit ae235b
    {  
Packit ae235b
      g_get_current_time (&now;;
Packit ae235b
      seed[0] = now.tv_sec;
Packit ae235b
      seed[1] = now.tv_usec;
Packit ae235b
      seed[2] = getpid ();
Packit ae235b
      seed[3] = getppid ();
Packit ae235b
    }
Packit ae235b
#else /* G_OS_WIN32 */
Packit ae235b
  /* rand_s() is only available since Visual Studio 2005 and
Packit ae235b
   * MinGW-w64 has a wrapper that will emulate rand_s() if it's not in msvcrt
Packit ae235b
   */
Packit ae235b
#if (defined(_MSC_VER) && _MSC_VER >= 1400) || defined(__MINGW64_VERSION_MAJOR)
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (seed); i++)
Packit ae235b
    rand_s (&seed[i]);
Packit ae235b
#else
Packit ae235b
#warning Using insecure seed for random number generation because of missing rand_s() in Windows XP
Packit ae235b
  GTimeVal now;
Packit ae235b
Packit ae235b
  g_get_current_time (&now;;
Packit ae235b
  seed[0] = now.tv_sec;
Packit ae235b
  seed[1] = now.tv_usec;
Packit ae235b
  seed[2] = getpid ();
Packit ae235b
  seed[3] = 0;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return g_rand_new_with_seed_array (seed, 4);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_free:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 *
Packit ae235b
 * Frees the memory allocated for the #GRand.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rand_free (GRand *rand)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (rand != NULL);
Packit ae235b
Packit ae235b
  g_free (rand);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_copy:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 *
Packit ae235b
 * Copies a #GRand into a new one with the same exact state as before.
Packit ae235b
 * This way you can take a snapshot of the random number generator for
Packit ae235b
 * replaying later.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GRand
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
GRand*
Packit ae235b
g_rand_copy (GRand *rand)
Packit ae235b
{
Packit ae235b
  GRand* new_rand;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (rand != NULL, NULL);
Packit ae235b
Packit ae235b
  new_rand = g_new0 (GRand, 1);
Packit ae235b
  memcpy (new_rand, rand, sizeof (GRand));
Packit ae235b
Packit ae235b
  return new_rand;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_set_seed:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 * @seed: a value to reinitialize the random number generator
Packit ae235b
 *
Packit ae235b
 * Sets the seed for the random number generator #GRand to @seed.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rand_set_seed (GRand   *rand,
Packit ae235b
                 guint32  seed)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (rand != NULL);
Packit ae235b
Packit ae235b
  switch (get_random_version ())
Packit ae235b
    {
Packit ae235b
    case 20:
Packit ae235b
      /* setting initial seeds to mt[N] using         */
Packit ae235b
      /* the generator Line 25 of Table 1 in          */
Packit ae235b
      /* [KNUTH 1981, The Art of Computer Programming */
Packit ae235b
      /*    Vol. 2 (2nd Ed.), pp102]                  */
Packit ae235b
      
Packit ae235b
      if (seed == 0) /* This would make the PRNG produce only zeros */
Packit ae235b
	seed = 0x6b842128; /* Just set it to another number */
Packit ae235b
      
Packit ae235b
      rand->mt[0]= seed;
Packit ae235b
      for (rand->mti=1; rand->mti<N; rand->mti++)
Packit ae235b
	rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]);
Packit ae235b
      
Packit ae235b
      break;
Packit ae235b
    case 22:
Packit ae235b
      /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
Packit ae235b
      /* In the previous version (see above), MSBs of the    */
Packit ae235b
      /* seed affect only MSBs of the array mt[].            */
Packit ae235b
      
Packit ae235b
      rand->mt[0]= seed;
Packit ae235b
      for (rand->mti=1; rand->mti<N; rand->mti++)
Packit ae235b
	rand->mt[rand->mti] = 1812433253UL * 
Packit ae235b
	  (rand->mt[rand->mti-1] ^ (rand->mt[rand->mti-1] >> 30)) + rand->mti; 
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_set_seed_array:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 * @seed: array to initialize with
Packit ae235b
 * @seed_length: length of array
Packit ae235b
 *
Packit ae235b
 * Initializes the random number generator by an array of longs.
Packit ae235b
 * Array can be of arbitrary size, though only the first 624 values
Packit ae235b
 * are taken.  This function is useful if you have many low entropy
Packit ae235b
 * seeds, or if you require more then 32 bits of actual entropy for
Packit ae235b
 * your application.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rand_set_seed_array (GRand         *rand,
Packit ae235b
                       const guint32 *seed,
Packit ae235b
                       guint          seed_length)
Packit ae235b
{
Packit ae235b
  int i, j, k;
Packit ae235b
Packit ae235b
  g_return_if_fail (rand != NULL);
Packit ae235b
  g_return_if_fail (seed_length >= 1);
Packit ae235b
Packit ae235b
  g_rand_set_seed (rand, 19650218UL);
Packit ae235b
Packit ae235b
  i=1; j=0;
Packit ae235b
  k = (N>seed_length ? N : seed_length);
Packit ae235b
  for (; k; k--)
Packit ae235b
    {
Packit ae235b
      rand->mt[i] = (rand->mt[i] ^
Packit ae235b
		     ((rand->mt[i-1] ^ (rand->mt[i-1] >> 30)) * 1664525UL))
Packit ae235b
	      + seed[j] + j; /* non linear */
Packit ae235b
      rand->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
Packit ae235b
      i++; j++;
Packit ae235b
      if (i>=N)
Packit ae235b
        {
Packit ae235b
	  rand->mt[0] = rand->mt[N-1];
Packit ae235b
	  i=1;
Packit ae235b
	}
Packit ae235b
      if (j>=seed_length)
Packit ae235b
	j=0;
Packit ae235b
    }
Packit ae235b
  for (k=N-1; k; k--)
Packit ae235b
    {
Packit ae235b
      rand->mt[i] = (rand->mt[i] ^
Packit ae235b
		     ((rand->mt[i-1] ^ (rand->mt[i-1] >> 30)) * 1566083941UL))
Packit ae235b
	      - i; /* non linear */
Packit ae235b
      rand->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
Packit ae235b
      i++;
Packit ae235b
      if (i>=N)
Packit ae235b
        {
Packit ae235b
	  rand->mt[0] = rand->mt[N-1];
Packit ae235b
	  i=1;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  rand->mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_boolean:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 *
Packit ae235b
 * Returns a random #gboolean from @rand_.
Packit ae235b
 * This corresponds to a unbiased coin toss.
Packit ae235b
 *
Packit ae235b
 * Returns: a random #gboolean
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * g_rand_int:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 *
Packit ae235b
 * Returns the next random #guint32 from @rand_ equally distributed over
Packit ae235b
 * the range [0..2^32-1].
Packit ae235b
 *
Packit ae235b
 * Returns: a random number
Packit ae235b
 */
Packit ae235b
guint32
Packit ae235b
g_rand_int (GRand *rand)
Packit ae235b
{
Packit ae235b
  guint32 y;
Packit ae235b
  static const guint32 mag01[2]={0x0, MATRIX_A};
Packit ae235b
  /* mag01[x] = x * MATRIX_A  for x=0,1 */
Packit ae235b
Packit ae235b
  g_return_val_if_fail (rand != NULL, 0);
Packit ae235b
Packit ae235b
  if (rand->mti >= N) { /* generate N words at one time */
Packit ae235b
    int kk;
Packit ae235b
    
Packit ae235b
    for (kk = 0; kk < N - M; kk++) {
Packit ae235b
      y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
Packit ae235b
      rand->mt[kk] = rand->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
Packit ae235b
    }
Packit ae235b
    for (; kk < N - 1; kk++) {
Packit ae235b
      y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
Packit ae235b
      rand->mt[kk] = rand->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
Packit ae235b
    }
Packit ae235b
    y = (rand->mt[N-1]&UPPER_MASK)|(rand->mt[0]&LOWER_MASK);
Packit ae235b
    rand->mt[N-1] = rand->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
Packit ae235b
    
Packit ae235b
    rand->mti = 0;
Packit ae235b
  }
Packit ae235b
  
Packit ae235b
  y = rand->mt[rand->mti++];
Packit ae235b
  y ^= TEMPERING_SHIFT_U(y);
Packit ae235b
  y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
Packit ae235b
  y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
Packit ae235b
  y ^= TEMPERING_SHIFT_L(y);
Packit ae235b
  
Packit ae235b
  return y; 
Packit ae235b
}
Packit ae235b
Packit ae235b
/* transform [0..2^32] -> [0..1] */
Packit ae235b
#define G_RAND_DOUBLE_TRANSFORM 2.3283064365386962890625e-10
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_int_range:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 * @begin: lower closed bound of the interval
Packit ae235b
 * @end: upper open bound of the interval
Packit ae235b
 *
Packit ae235b
 * Returns the next random #gint32 from @rand_ equally distributed over
Packit ae235b
 * the range [@begin..@end-1].
Packit ae235b
 *
Packit ae235b
 * Returns: a random number
Packit ae235b
 */
Packit ae235b
gint32 
Packit ae235b
g_rand_int_range (GRand  *rand,
Packit ae235b
                  gint32  begin,
Packit ae235b
                  gint32  end)
Packit ae235b
{
Packit ae235b
  guint32 dist = end - begin;
Packit ae235b
  guint32 random;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (rand != NULL, begin);
Packit ae235b
  g_return_val_if_fail (end > begin, begin);
Packit ae235b
Packit ae235b
  switch (get_random_version ())
Packit ae235b
    {
Packit ae235b
    case 20:
Packit ae235b
      if (dist <= 0x10000L) /* 2^16 */
Packit ae235b
	{
Packit ae235b
	  /* This method, which only calls g_rand_int once is only good
Packit ae235b
	   * for (end - begin) <= 2^16, because we only have 32 bits set
Packit ae235b
	   * from the one call to g_rand_int ().
Packit ae235b
	   *
Packit ae235b
	   * We are using (trans + trans * trans), because g_rand_int only
Packit ae235b
	   * covers [0..2^32-1] and thus g_rand_int * trans only covers
Packit ae235b
	   * [0..1-2^-32], but the biggest double < 1 is 1-2^-52. 
Packit ae235b
	   */
Packit ae235b
	  
Packit ae235b
	  gdouble double_rand = g_rand_int (rand) * 
Packit ae235b
	    (G_RAND_DOUBLE_TRANSFORM +
Packit ae235b
	     G_RAND_DOUBLE_TRANSFORM * G_RAND_DOUBLE_TRANSFORM);
Packit ae235b
	  
Packit ae235b
	  random = (gint32) (double_rand * dist);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  /* Now we use g_rand_double_range (), which will set 52 bits
Packit ae235b
	   * for us, so that it is safe to round and still get a decent
Packit ae235b
	   * distribution
Packit ae235b
           */
Packit ae235b
	  random = (gint32) g_rand_double_range (rand, 0, dist);
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    case 22:
Packit ae235b
      if (dist == 0)
Packit ae235b
	random = 0;
Packit ae235b
      else 
Packit ae235b
	{
Packit ae235b
	  /* maxvalue is set to the predecessor of the greatest
Packit ae235b
	   * multiple of dist less or equal 2^32.
Packit ae235b
	   */
Packit ae235b
	  guint32 maxvalue;
Packit ae235b
	  if (dist <= 0x80000000u) /* 2^31 */
Packit ae235b
	    {
Packit ae235b
	      /* maxvalue = 2^32 - 1 - (2^32 % dist) */
Packit ae235b
	      guint32 leftover = (0x80000000u % dist) * 2;
Packit ae235b
	      if (leftover >= dist) leftover -= dist;
Packit ae235b
	      maxvalue = 0xffffffffu - leftover;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    maxvalue = dist - 1;
Packit ae235b
	  
Packit ae235b
	  do
Packit ae235b
	    random = g_rand_int (rand);
Packit ae235b
	  while (random > maxvalue);
Packit ae235b
	  
Packit ae235b
	  random %= dist;
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      random = 0;		/* Quiet GCC */
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }      
Packit ae235b
 
Packit ae235b
  return begin + random;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_double:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 *
Packit ae235b
 * Returns the next random #gdouble from @rand_ equally distributed over
Packit ae235b
 * the range [0..1).
Packit ae235b
 *
Packit ae235b
 * Returns: a random number
Packit ae235b
 */
Packit ae235b
gdouble 
Packit ae235b
g_rand_double (GRand *rand)
Packit ae235b
{    
Packit ae235b
  /* We set all 52 bits after the point for this, not only the first
Packit ae235b
     32. Thats why we need two calls to g_rand_int */
Packit ae235b
  gdouble retval = g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM;
Packit ae235b
  retval = (retval + g_rand_int (rand)) * G_RAND_DOUBLE_TRANSFORM;
Packit ae235b
Packit ae235b
  /* The following might happen due to very bad rounding luck, but
Packit ae235b
   * actually this should be more than rare, we just try again then */
Packit ae235b
  if (retval >= 1.0) 
Packit ae235b
    return g_rand_double (rand);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rand_double_range:
Packit ae235b
 * @rand_: a #GRand
Packit ae235b
 * @begin: lower closed bound of the interval
Packit ae235b
 * @end: upper open bound of the interval
Packit ae235b
 *
Packit ae235b
 * Returns the next random #gdouble from @rand_ equally distributed over
Packit ae235b
 * the range [@begin..@end).
Packit ae235b
 *
Packit ae235b
 * Returns: a random number
Packit ae235b
 */
Packit ae235b
gdouble 
Packit ae235b
g_rand_double_range (GRand   *rand,
Packit ae235b
                     gdouble  begin,
Packit ae235b
                     gdouble  end)
Packit ae235b
{
Packit ae235b
  gdouble r;
Packit ae235b
Packit ae235b
  r = g_rand_double (rand);
Packit ae235b
Packit ae235b
  return r * end - (r - 1) * begin;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GRand *
Packit ae235b
get_global_random (void)
Packit ae235b
{
Packit ae235b
  static GRand *global_random;
Packit ae235b
Packit ae235b
  /* called while locked */
Packit ae235b
  if (!global_random)
Packit ae235b
    global_random = g_rand_new ();
Packit ae235b
Packit ae235b
  return global_random;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_random_boolean:
Packit ae235b
 *
Packit ae235b
 * Returns a random #gboolean.
Packit ae235b
 * This corresponds to a unbiased coin toss.
Packit ae235b
 *
Packit ae235b
 * Returns: a random #gboolean
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * g_random_int:
Packit ae235b
 *
Packit ae235b
 * Return a random #guint32 equally distributed over the range
Packit ae235b
 * [0..2^32-1].
Packit ae235b
 *
Packit ae235b
 * Returns: a random number
Packit ae235b
 */
Packit ae235b
guint32
Packit ae235b
g_random_int (void)
Packit ae235b
{
Packit ae235b
  guint32 result;
Packit ae235b
  G_LOCK (global_random);
Packit ae235b
  result = g_rand_int (get_global_random ());
Packit ae235b
  G_UNLOCK (global_random);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_random_int_range:
Packit ae235b
 * @begin: lower closed bound of the interval
Packit ae235b
 * @end: upper open bound of the interval
Packit ae235b
 *
Packit ae235b
 * Returns a random #gint32 equally distributed over the range
Packit ae235b
 * [@begin..@end-1].
Packit ae235b
 *
Packit ae235b
 * Returns: a random number
Packit ae235b
 */
Packit ae235b
gint32 
Packit ae235b
g_random_int_range (gint32 begin,
Packit ae235b
                    gint32 end)
Packit ae235b
{
Packit ae235b
  gint32 result;
Packit ae235b
  G_LOCK (global_random);
Packit ae235b
  result = g_rand_int_range (get_global_random (), begin, end);
Packit ae235b
  G_UNLOCK (global_random);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_random_double:
Packit ae235b
 *
Packit ae235b
 * Returns a random #gdouble equally distributed over the range [0..1).
Packit ae235b
 *
Packit ae235b
 * Returns: a random number
Packit ae235b
 */
Packit ae235b
gdouble 
Packit ae235b
g_random_double (void)
Packit ae235b
{
Packit ae235b
  double result;
Packit ae235b
  G_LOCK (global_random);
Packit ae235b
  result = g_rand_double (get_global_random ());
Packit ae235b
  G_UNLOCK (global_random);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_random_double_range:
Packit ae235b
 * @begin: lower closed bound of the interval
Packit ae235b
 * @end: upper open bound of the interval
Packit ae235b
 *
Packit ae235b
 * Returns a random #gdouble equally distributed over the range
Packit ae235b
 * [@begin..@end).
Packit ae235b
 *
Packit ae235b
 * Returns: a random number
Packit ae235b
 */
Packit ae235b
gdouble 
Packit ae235b
g_random_double_range (gdouble begin,
Packit ae235b
                       gdouble end)
Packit ae235b
{
Packit ae235b
  double result;
Packit ae235b
  G_LOCK (global_random);
Packit ae235b
  result = g_rand_double_range (get_global_random (), begin, end);
Packit ae235b
  G_UNLOCK (global_random);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_random_set_seed:
Packit ae235b
 * @seed: a value to reinitialize the global random number generator
Packit ae235b
 * 
Packit ae235b
 * Sets the seed for the global random number generator, which is used
Packit ae235b
 * by the g_random_* functions, to @seed.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_random_set_seed (guint32 seed)
Packit ae235b
{
Packit ae235b
  G_LOCK (global_random);
Packit ae235b
  g_rand_set_seed (get_global_random (), seed);
Packit ae235b
  G_UNLOCK (global_random);
Packit ae235b
}