Reference Manual

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# Inti::G::Rand Class Reference

A GRand C++ wrapper interface. More...

`#include <inti/glib/rand.h>`

Inheritance diagram for Inti::G::Rand:

List of all members.

## Public Member Functions

Constructors

- Rand ()
    Constructs a new random number generator initialized with a seed taken either from /dev/urandom (if existing) or from the current time (as a fallback).
- Rand (unsigned int seed)
    Constructs a new random number generator initialized with seed.
- ~Rand ()
    Destructor.

Accessors

- GRand * g_rand () const
    Get a pointer to the GRand object.
- operator GRand * () const
    Conversion operator; Safely converts a G::Rand object into a GRand pointer.
- bool get_bool () const
    Gets a random boolean value (this corresponds to a unbiased coin toss).
- unsigned int get_int () const
    Gets the next random unsigned integer from the rand equally distributed over the range [0..2^32-1].
- int get_int_range (int begin, int end) const
    Gets the next random integer from the rand equally distributed over the range [begin..end-1].
- double get_double () const
    Gets the next random double from the rand equally distributed over the range [0..1).
- double get_double_range (double begin, double end) const
    Gets the next random double from the rand equally distributed over the range [begin..end).

Methods

- void set_seed (unsigned int seed)
    Sets the seed for the random number generator to seed.

## Detailed Description

A GRand C++ wrapper interface.

Rand is a portable, fast and good pseudo-random number generator (PRNG). It uses the Mersenne Twister PRNG, which was originally developed by Makoto Matsumoto and Takuji Nishimura. Further information can be found at http://www.math.keio.ac.jp/~matumoto/emt.html.

If you just need a random number, you can simply call the G::random* methods, which will create a globally used Rand object and use its methods internally. Whenever you need a stream of reproducible random numbers, you should create a Rand object yourself and call its methods directly, which will also be slightly faster. Initializing a Rand with a certain seed will produce exactly the same series of random numbers on all platforms. This can thus be used as a seed for games.

The Rand range methods will return high quality equally distributed random numbers, whereas for example the (G::Random::get_int()max) approach often doesn't yield equally distributed numbers.

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. Also the pseudo-random integers generated by Rand range methods will have a slightly better equal distribution with the new version of GLib.

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 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.

## Constructor & Destructor Documentation

Inti::G::Rand::Rand | ( | unsigned int | seed | ) |

  | Constructs a new random number generator initialized with seed. Parameters:
  seed | A value to initialize the random number generator.

## Member Function Documentation

bool Inti::G::Rand::get_bool | ( |  | ) | const

| Gets a random boolean value (this corresponds to a unbiased coin toss). Returns: A random bool value.

double Inti::G::Rand::get_double | ( |  | ) | const

| Gets the next random double from the rand equally distributed over the range [0..1). Returns: A random number.

double Inti::G::Rand::get_double_range | ( | double | begin,
|  | double | end
| ) | const

  | Gets the next random double from the rand equally distributed over the range [begin..end). Parameters:
  begin | The lower closed bound of the interval.
  end | The upper open bound of the interval.
Returns: A random number.

unsigned int Inti::G::Rand::get_int | ( |  | ) | const

| Gets the next random unsigned integer from the rand equally distributed over the range [0..2^32-1]. Returns: A random number.

int Inti::G::Rand::get_int_range | ( | int | begin,
|  | int | end
| ) | const

  | Gets the next random integer from the rand equally distributed over the range [begin..end-1]. Parameters:
  begin | The lower closed bound of the interval.
  end | The upper open bound of the interval.
Returns: A random number.

void Inti::G::Rand::set_seed | ( | unsigned int | seed | ) |

  | Sets the seed for the random number generator to seed. Parameters:
  seed | A value to reinitialize the random number generator.

The documentation for this class was generated from the following file:

- rand.h

Main Page - Footer