plll  1.0
plll::arithmetic::RandomNumberGenerator Class Reference

Represents a random number generator. More...

#include <arithmetic-gmp.hpp>

Public Member Functions

Integer getSeed () const
 Retrieves the last set seed of the generator. More...
 
RandomNumberGeneratoroperator= (const RandomNumberGenerator &rng)
 Copies the state of the given random number generator to the current one. More...
 
void random (Integer &res, const Integer &bound)
 Creates a random arbitrary precision integer in the range $[0, bound)$. More...
 
Integer random (const Integer &bound)
 Creates and returns a random arbitrary precision integer in the range $[0, bound)$. More...
 
unsigned long random (unsigned long bound)
 Creates and returns a native integer in the range $[0, bound)$. More...
 
void randomBits (void *ptr, unsigned long count)
 Writes a given number of random bytes to the memory location pointed to. More...
 
void randomBits (Integer &res, unsigned long bits)
 Creates random bits. More...
 
Integer randomBits (unsigned long bits)
 Creates random bits. More...
 
void randomizeDevRandom (double goodness=0.01)
 Uses the operating system's random number generator to initialize this random number generator. More...
 
void randomizeSeed ()
 Initializes this random number generator with a random seed. More...
 
void randomizeTime ()
 Uses the system's time to seed this random number generator. More...
 
void randomLen (Integer &res, unsigned long bits)
 Creates a random integer of a fixed bit length. More...
 
Integer randomLen (unsigned long bits)
 Creates a random integer of a fixed bit length. More...
 
 RandomNumberGenerator ()
 Creates a new default initialized random number generator.
 
 RandomNumberGenerator (const RandomNumberGenerator &)
 Creates a clone of the given random number generator.
 
void randomUniform (Real &r)
 Creates a uniformly distributed floating point number in the interval $[0, 1)$. More...
 
void randomUniform (Real &r, const RealContext &rc)
 Creates a uniformly distributed floating point number in the interval $[0, 1)$. More...
 
Real randomUniform ()
 Creates and returns a uniformly distributed floating point number in the interval $[0, 1)$. More...
 
Real randomUniform (const RealContext &rc)
 Creates and returns a uniformly distributed floating point number in the interval $[0, 1)$. More...
 
void setSeed (const Integer &seed)
 Sets a new seed for the generator. More...
 
 ~RandomNumberGenerator ()
 Releases the state for this random number generator.
 

Static Public Member Functions

static Integer createSeed ()
 Creates a seed using an internal random number generator. More...
 
static void initializeSeeder (double goodness=0.01)
 Calls randomizeDevRandom(goodness) for the internal random number generator which is used for createSeed(). More...
 

Detailed Description

Represents a random number generator.

As the underlying generator, the Mersenne Twister is used.

Warning
Note that while this random number generator is perfectly fine for most purposes, it is not for cryptographical purposes.

Definition at line 3281 of file arithmetic-gmp.hpp.

Member Function Documentation

static Integer plll::arithmetic::RandomNumberGenerator::createSeed ( )
static

Creates a seed using an internal random number generator.

The internal random number generator is initialized using /dev/random and /dev/urandom on the first call to this function.

Returns
A seed for use with setSeed().
Warning
Might block on the first call. (But only then, and this can be avoided by calling RandomNumberGenerator::randomizeSeed() earlier.)
See also
RandomNumberGenerator::randomizeSeed()
Integer plll::arithmetic::RandomNumberGenerator::getSeed ( ) const

Retrieves the last set seed of the generator.

Returns
The previously set seed.
static void plll::arithmetic::RandomNumberGenerator::initializeSeeder ( double  goodness = 0.01)
static

Calls randomizeDevRandom(goodness) for the internal random number generator which is used for createSeed().

Allows to initialize the internal random number generator at this point of time. If RandomNumberGenerator::createSeed() is then called later during the program, no blocking will occur at that point.

Warning
This call might block until enough entropy is available to the operating system.
See also
RandomNumberGenerator::createSeed()
RandomNumberGenerator& plll::arithmetic::RandomNumberGenerator::operator= ( const RandomNumberGenerator rng)

Copies the state of the given random number generator to the current one.

Parameters
rngThe random number generator whose state should be copied.
Returns
A reference to the current random number generator.
void plll::arithmetic::RandomNumberGenerator::random ( Integer res,
const Integer bound 
)

Creates a random arbitrary precision integer in the range $[0, bound)$.

Parameters
resWhere to store the result.
boundA bound on the maximal integer returned.
Integer plll::arithmetic::RandomNumberGenerator::random ( const Integer bound)

Creates and returns a random arbitrary precision integer in the range $[0, bound)$.

Parameters
boundA bound on the maximal integer returned.
Returns
The result.
unsigned long plll::arithmetic::RandomNumberGenerator::random ( unsigned long  bound)

Creates and returns a native integer in the range $[0, bound)$.

Parameters
boundA bound on the maximal integer returned.
Returns
The result.
void plll::arithmetic::RandomNumberGenerator::randomBits ( void *  ptr,
unsigned long  count 
)

Writes a given number of random bytes to the memory location pointed to.

Parameters
ptrA pointer to the memory location.
countThe number of bytes to write there.
void plll::arithmetic::RandomNumberGenerator::randomBits ( Integer res,
unsigned long  bits 
)

Creates random bits.

Parameters
resWill be filled with a random integer in range $[0, 2^{bits})$.
bitsThe number of bits.
Integer plll::arithmetic::RandomNumberGenerator::randomBits ( unsigned long  bits)

Creates random bits.

Parameters
bitsThe number of bits.
Returns
A random integer in range $[0, 2^{bits})$.
void plll::arithmetic::RandomNumberGenerator::randomizeDevRandom ( double  goodness = 0.01)

Uses the operating system's random number generator to initialize this random number generator.

Parameters
goodnessSpecify how much of the state should be taken from /dev/random instead of /dev/urandom. The default fraction is very small, to minimize the amount of blocking when trying to read too much from /dev/random. A value of 0.0 reads everything from /dev/urandom, and thus might be completely predictable to a watcher, while a value of 1.0 reads everything from /dev/random, resulting in a state completely independent from previously read random values.

For most purposes, the default value 0.01 is totally fine, and even 0.0 is fine in many cases. Note that only a value of 0.0 garuantees that absolutely no blocking occurs.

void plll::arithmetic::RandomNumberGenerator::randomizeSeed ( )

Initializes this random number generator with a random seed.

Equivalent to setSeed(RandomNumberGenerator::createSeed()).

void plll::arithmetic::RandomNumberGenerator::randomizeTime ( )

Uses the system's time to seed this random number generator.

Warning
This is implementation dependent and might behave completely different even on the same system if two programs are run at the precisely same time.
void plll::arithmetic::RandomNumberGenerator::randomLen ( Integer res,
unsigned long  bits 
)

Creates a random integer of a fixed bit length.

Parameters
resWill be filled with a random integer in range $[2^{bits-1}, 2^{bits})$.
bitsThe number of bits.
Integer plll::arithmetic::RandomNumberGenerator::randomLen ( unsigned long  bits)

Creates a random integer of a fixed bit length.

Parameters
bitsThe number of bits.
Returns
A random integer in range $[2^{bits-1}, 2^{bits})$.
void plll::arithmetic::RandomNumberGenerator::randomUniform ( Real r)

Creates a uniformly distributed floating point number in the interval $[0, 1)$.

Parameters
rWhere to store the result.
void plll::arithmetic::RandomNumberGenerator::randomUniform ( Real r,
const RealContext rc 
)

Creates a uniformly distributed floating point number in the interval $[0, 1)$.

Parameters
rWhere to store the result.
rcThe context whose precision to use for the result.
Real plll::arithmetic::RandomNumberGenerator::randomUniform ( )
inline

Creates and returns a uniformly distributed floating point number in the interval $[0, 1)$.

Returns
The random number.

Definition at line 3476 of file arithmetic-gmp.hpp.

Real plll::arithmetic::RandomNumberGenerator::randomUniform ( const RealContext rc)
inline

Creates and returns a uniformly distributed floating point number in the interval $[0, 1)$.

Parameters
rcThe context whose precision to use for the result.
Returns
The random number.

Definition at line 3490 of file arithmetic-gmp.hpp.

void plll::arithmetic::RandomNumberGenerator::setSeed ( const Integer seed)

Sets a new seed for the generator.

The state is generated from this seed, so a short or small seed does not lead to a bad state.

Parameters
seedThe integer to use as a seed.

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