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

Public Types

enum [anonymous]


enumerator N
enum [anonymous]


enumerator SAVE

Public Functions

Random(const uint32_t &oneSeed)

initialize with a simple uint32_t

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

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)

uint32_t randInt()

integer in [0,2**32-1]

uint32_t randInt(const uint32_t &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

inline double operator()()

same as rand()

double rand53()

real number in [0,1) (capacity of IEEE double precision) Exponential distribution in (0,inf)

double randExponential()
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 randVectorLamberts()

Random lamberts distributed vector with theta distribution: sin(t) * cos(t), aka cosine law (, for a surface element with normal vector pointing in positive z-axis (0, 0, 1)

Vector3d randVectorLamberts(const Vector3d &normalVector)

Same as above but rotated to the respective normalVector of surface element.

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_t oneSeed)

Seed the generator with a simple uint32_t.

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

Seed the generator with an array of uint32_t’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_t’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(const std::string &b64Seed)
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_t *saveArray) const
void load(uint32_t *const loadArray)
const std::vector<uint32_t> &getSeed() const
const std::string getSeed_base64() const

Public Static Functions

static Random &instance()
static void seedThreads(const uint32_t oneSeed)
static std::vector<std::vector<uint32_t>> getSeedThreads()

Protected Types

enum [anonymous]


enumerator M

Protected Functions

void initialize(const uint32_t 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 (

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

Protected Attributes

uint32_t state[N]
std::vector<uint32_t> initial_seed
uint32_t *pNext
int left

Protected Static Functions

static uint32_t hash(time_t t, clock_t c)

Get a uint32_t from t and c Better than uint32_t(x) in case x is floating point in [0,1] Based on code by Lawrence Kirby (


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