tvm
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
tvm::support::LinearCongruentialEngine Class Reference

This linear congruential engine is a drop-in replacement for std::minstd_rand. It strictly corresponds to std::minstd_rand and is designed to be platform-independent. More...

#include <random_engine.h>

Collaboration diagram for tvm::support::LinearCongruentialEngine:

Public Types

using TRandState = int64_t
 
using result_type = uint64_t
 The result type. More...
 

Public Member Functions

result_type operator() ()
 Operator to move the random state to the next and return the new random state. According to definition of linear congruential engine, the new random state value is computed as new_random_state = (current_random_state * multiplier + increment) % modulus. More...
 
void Seed (TRandState rand_state)
 Change the start random state of RNG with the seed of a new random state value. More...
 
TRandState ForkSeed ()
 Fork a new seed for another RNG from current random state. More...
 
 LinearCongruentialEngine (TRandState *rand_state_ptr)
 Construct a random number generator with a random state pointer. More...
 

Static Public Member Functions

static constexpr result_type min ()
 The minimum possible value of random state here. More...
 
static constexpr result_type max ()
 The maximum possible value of random state here. More...
 
static TRandState DeviceRandom ()
 Get a device random state. More...
 
static TRandState NormalizeSeed (TRandState rand_state)
 Normalize the random seed to the range of [1, modulus - 1]. More...
 

Static Public Attributes

static constexpr TRandState multiplier = 48271
 The multiplier. More...
 
static constexpr TRandState increment = 0
 The increment. More...
 
static constexpr TRandState modulus = 2147483647
 The modulus. More...
 

Detailed Description

This linear congruential engine is a drop-in replacement for std::minstd_rand. It strictly corresponds to std::minstd_rand and is designed to be platform-independent.

Note
Our linear congruential engine is a complete implementation of std::uniform_random_bit_generator so it can be used as generator for any STL random number distribution. However, parts of std::linear_congruential_engine's member functions are not included for simplification. For full member functions of std::minstd_rand, please check out the following link: https://en.cppreference.com/w/cpp/numeric/random/linear_congruential_engine

Member Typedef Documentation

◆ result_type

The result type.

◆ TRandState

Constructor & Destructor Documentation

◆ LinearCongruentialEngine()

tvm::support::LinearCongruentialEngine::LinearCongruentialEngine ( TRandState rand_state_ptr)
inlineexplicit

Construct a random number generator with a random state pointer.

Parameters
rand_state_ptrThe random state pointer given in result_type*.
Note
The random state is not checked for whether it's nullptr and whether it's in the range of [0, modulus-1]. We assume the given random state is valid or the Seed function would be called right after the constructor before any usage.

Member Function Documentation

◆ DeviceRandom()

static TRandState tvm::support::LinearCongruentialEngine::DeviceRandom ( )
inlinestatic

Get a device random state.

Returns
The random state

◆ ForkSeed()

TRandState tvm::support::LinearCongruentialEngine::ForkSeed ( )
inline

Fork a new seed for another RNG from current random state.

Returns
The forked seed.

◆ max()

static constexpr result_type tvm::support::LinearCongruentialEngine::max ( )
inlinestaticconstexpr

The maximum possible value of random state here.

◆ min()

static constexpr result_type tvm::support::LinearCongruentialEngine::min ( )
inlinestaticconstexpr

The minimum possible value of random state here.

◆ NormalizeSeed()

static TRandState tvm::support::LinearCongruentialEngine::NormalizeSeed ( TRandState  rand_state)
inlinestatic

Normalize the random seed to the range of [1, modulus - 1].

Parameters
rand_stateThe random seed.
Returns
The normalized random seed.

◆ operator()()

result_type tvm::support::LinearCongruentialEngine::operator() ( )
inline

Operator to move the random state to the next and return the new random state. According to definition of linear congruential engine, the new random state value is computed as new_random_state = (current_random_state * multiplier + increment) % modulus.

Returns
The next current random state value in the type of result_type.
Note
In order for better efficiency, the implementation here has a few assumptions:
  1. The multiplication and addition won't overflow.
  2. The given random state pointer rand_state_ptr is not nullptr.
  3. The given random state *(rand_state_ptr) is in the range of [0, modulus - 1].

◆ Seed()

void tvm::support::LinearCongruentialEngine::Seed ( TRandState  rand_state)
inline

Change the start random state of RNG with the seed of a new random state value.

Parameters
rand_stateThe random state given in result_type.

Member Data Documentation

◆ increment

constexpr TRandState tvm::support::LinearCongruentialEngine::increment = 0
staticconstexpr

The increment.

◆ modulus

constexpr TRandState tvm::support::LinearCongruentialEngine::modulus = 2147483647
staticconstexpr

The modulus.

◆ multiplier

constexpr TRandState tvm::support::LinearCongruentialEngine::multiplier = 48271
staticconstexpr

The multiplier.


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