CGAL::Random

Definition

The class Randomis a random numbers generator. It generates uniformly distributed random bools, ints and doubles. It can be used as the random number generating function object in the STL algorithm random_shuffle.

Instances of Random can be seen as input streams. Different streams are independent of each other, i.e. the sequence of numbers from one stream does not depend upon how many numbers were extracted from the other streams. At each time, an instance has a state that uniquely determines the subsequent numbers being produced.

It can be very useful, e.g. for debugging, to reproduce a sequence of random numbers. This can be done by either initialising with a fixed seed, or by using the state functions as described below.

#include <CGAL/Random.h>

Types

Random::State
The State type.

Creation

Random random;
introduces a variable random of type Random. The seed is chosen ``randomly'', depending on the system time.


Random random ( unsigned int seed);
introduces a variable random of type Random and initializes its internal state using seed. Equal values for seed result in equal sequences of random numbers.

Operations

bool random.get_bool () returns a random bool.

template <int b>
int random.get_bits () returns a random int value from the interval [0,2^b). This is supposed to be efficient.

int random.get_int ( int lower, int upper)
returns a random int from the interval [lower,upper).

double random.get_double ( double lower = 0.0, double upper = 1.0)
returns a random double from the interval [lower,upper).

int random ( int upper ) returns random.get_int( 0, upper).

Seed and State Functions

unsigned int random.get_seed () returns the seed used for initialization.

void random.save_state ( State& state) saves the current internal state in state.

void random.restore_state ( State state)
restores the internal state from state.

Equality Test

bool random == random2 returns true, iff random and random2 have equal internal states.

Implementation

We use the C library function rand to generate the random numbers, i.e., the sequence of numbers depends on the implementation of rand on your specific platform.

See Also

CGAL::default_random