Skip to main content

Mersenne RNG

The Mersenne generator provides better "randomness" compared to Marsaglia but is also more computationally intensive. The generator uses just one seed compared to Marsaglia's two.

Constructor

// #1 - use a random seed based on time-of-day
// NOTE: this will create variation between runs and between platforms.
MersenneRNG::MersenneRNG();

// #2 - use explicitly defined seed
MersenneRNG::MersenneRNG(unsigned int seed);

Create a new Mersenne random number generator.

Parameters

  • returns None

generateNextInt32

int32_t generateNextInt32();

Generate the next signed 32-bit integer.

Parameters

  • returns (int32_t) The next signed 32-bit integer.

generateNextInt64

int64_t generateNextInt64();

Generate the next signed 64-bit integer.

Parameters

  • returns (int64_t) The next signed 64-bit integer.

generateNextUInt32

uint32_t generateNextUInt32();

Generate the next unsigned 32-bit integer.

Parameters

  • returns (uint32_t) The next unsigned 32-bit integer.

generateNextUInt64

uint64_t generateNextUInt64();

Generate the next unsigned 64-bit integer.

Parameters

  • returns (uint64_t) The next unsigned 64-bit integer

nextUniform

double  nextUniform();

Generate the next double within the range [0,1).

Parameters

  • returns (double) The next double within the range [0,1).

seed

void seed(uint64_t newSeed);

Re-seed the random number generator.

Parameters

  • newSeed (uint64_t) A seed.
  • returns None

Examples

Example 1: generateNextInt32()

SST::RNG::MersenneRNG* rng = new SST::RNG::MersenneRNG(11);

int rndNumber;
rndNumber = (int)(rng->generateNextInt32());
rndNumber = (rndNumber & 0x0000FFFF) ^ ((rndNumber & 0xFFFF0000) >> 16);
rndNumber = abs((int)(rndNumber % 3));

Example 2: generateNextUInt32()

SST::RNG::MersenneRNG randomGenerator = new SST::RNG::MersenneRNG(11);

int rndNumber;
int index = randomGenerator.generateNextUInt32() % (numWays-1);

Example 3: generateNextUInt64()

SST::RNG::MersenneRNG rng = new SST::RNG::MersenneRNG(11);

uint64_t selectA = (rng.generateNextUInt64() % pageCount);
uint64_t selectB = (rng.generateNextUInt64() % pageCount);
#include <sst/core/rng/mersenne.h>