Class Random

Class Documentation

class Random

Random number generator.

Mersenne Twister random number generator a C++ class Random Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus Richard J. Wagner v1.0 15 May 2003 rjwagner@writeme.com

Public Types

enum [anonymous]

Values:

N = 624
enum [anonymous]

Values:

SAVE = N + 1
typedef unsigned long uint32

Public Functions

Random(const uint32 &oneSeed)

initialize with a simple uint32

Random(uint32 *const bigSeed, uint32 const seedLength = N)
Random()

auto-initialize with /dev/urandom or time() and clock() Do NOT use for CRYPTOGRAPHY without securely hashing several returned values together, otherwise the generator state can be learned after reading 624 consecutive values.

double rand()

real number in [0,1]

double rand(const double &n)

real number in [0,n]

double randExc()

real number in [0,1)

double randExc(const double &n)

real number in [0,n)

double randDblExc()

real number in (0,1)

double randDblExc(const double &n)

real number in (0,n) Pull a 32-bit integer from the generator state Every other access function simply transforms the numbers extracted here

Random::uint32 randInt()

integer in [0,2^32-1]

Random::uint32 randInt(const uint32 &n)

integer in [0,n] for n < 2^32

uint64_t randInt64()

integer in [0, 2**64 -1]. PROBABLY NOT SECURE TO USE

uint64_t randInt64(const uint64_t &n)

integer in [0, n] for n < 2**64 -1. PROBABLY NOT SECURE TO USE

double rand53()

Access to 53-bit random numbers (capacity of IEEE double precision)

double randExponential()

Exponential distribution in (0,inf)

double randNorm(const double &mean = 0.0, const double &variance = 1.0)

Normal distributed random number.

double randUniform(double min, double max)

Uniform distribution in [min, max].

double randRayleigh(double sigma)

Rayleigh distributed random number.

double randFisher(double k)

Fisher distributed random number.

size_t randBin(const std::vector<float> &cdf)

Draw a random bin from a (unnormalized) cumulative distribution function, without leading zero.

size_t randBin(const std::vector<double> &cdf)
Vector3d randVector()

Random point on a unit-sphere.

Vector3d randVectorAroundMean(const Vector3d &meanDirection, double angle)

Random vector with given angular separation around mean direction.

Vector3d randFisherVector(const Vector3d &meanDirection, double kappa)

Fisher distributed random vector.

Vector3d randConeVector(const Vector3d &meanDirection, double angularRadius)

Uniform distributed random vector inside a cone.

Vector3d randomInterpolatedPosition(const Vector3d &a, const Vector3d &b)

_Position vector uniformly distributed within propagation step size bin

double randPowerLaw(double index, double min, double max)

Power-law distribution of a given differential spectral index.

double randBrokenPowerLaw(double index1, double index2, double breakpoint, double min, double max)

Broken power-law distribution.

void seed(const uint32 oneSeed)

Seed the generator with a simple uint32.

void seed(uint32 *const bigSeed, const uint32 seedLength = N)

Seed the generator with an array of uint32’s There are 2^19937-1 possible initial states. This function allows all of those to be accessed by providing at least 19937 bits (with a default seed length of N = 624 uint32’s). Any bits above the lower 32 in each element are discarded. Just call seed() if you want to get array from /dev/urandom.

void seed()

Seed the generator with an array from /dev/urandom if available Otherwise use a hash of time() and clock() values.

void save(uint32 *saveArray) const
void load(uint32 *const loadArray)

Public Static Functions

Random &instance()
void seedThreads(const uint32 oneSeed)

Protected Types

enum [anonymous]

Values:

M = 397

Protected Functions

void initialize(const uint32 oneSeed)

Initialize generator state with seed See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier. In previous versions, most significant bits (MSBs) of the seed affect only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto.

void reload()

Generate N new values in state Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)

uint32 hiBit(const uint32 &u) const
uint32 loBit(const uint32 &u) const
uint32 loBits(const uint32 &u) const
uint32 mixBits(const uint32 &u, const uint32 &v) const
uint32 twist(const uint32 &m, const uint32 &s0, const uint32 &s1) const

Protected Attributes

uint32 state[N]
uint32 *pNext
int left

Protected Static Functions

Random::uint32 hash(time_t t, clock_t c)

Get a uint32 from t and c Better than uint32(x) in case x is floating point in [0,1] Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)

Friends

std::ostream &operator<<(std::ostream &os, const Random &mtrand)
std::istream &operator>>(std::istream &is, Random &mtrand)