openGPMP
Open Source Mathematics Package
Public Types | Public Member Functions | Private Attributes | List of all members
gpmp::core::rndm::LCG Class Reference

Linear Congruential Generator. More...

#include <random.hpp>

Public Types

using result_type = uint64_t
 

Public Member Functions

 LCG ()
 
 LCG (uint64_t seed, uint64_t a=6364136223846793005ULL, uint64_t c=1442695040888963407ULL)
 
uint64_t operator() ()
 
void seed (uint64_t new_seed)
 
uint64_t get_multiplier () const
 
uint64_t get_increment () const
 
uint64_t get_seed () const
 

Private Attributes

uint64_t state
 
uint64_t multiplier
 
uint64_t increment
 

Detailed Description

Linear Congruential Generator.

Parameters
m= modulus : 32-bit int max
a= multiplier : 6364136223846793005 (from Knuth MMIX)
c= increment : 1442695040888963407 (from Knuth MMIX)
Returns
generated number

Definition at line 82 of file random.hpp.

Member Typedef Documentation

◆ result_type

Definition at line 84 of file random.hpp.

Constructor & Destructor Documentation

◆ LCG() [1/2]

gpmp::core::rndm::LCG::LCG ( )

Definition at line 39 of file random.cpp.

40  : state(1), multiplier(6364136223846793005ULL),
41  increment(1442695040888963407ULL) {
42 }

◆ LCG() [2/2]

gpmp::core::rndm::LCG::LCG ( uint64_t  seed,
uint64_t  a = 6364136223846793005ULL,
uint64_t  c = 1442695040888963407ULL 
)

Definition at line 45 of file random.cpp.

46  : state(seed), multiplier(a), increment(c) {
47 }
void seed(uint64_t new_seed)
Definition: random.cpp:66

Member Function Documentation

◆ get_increment()

uint64_t gpmp::core::rndm::LCG::get_increment ( ) const

Definition at line 75 of file random.cpp.

75  {
76  return increment;
77 }

◆ get_multiplier()

uint64_t gpmp::core::rndm::LCG::get_multiplier ( ) const

Definition at line 71 of file random.cpp.

71  {
72  return multiplier;
73 }

◆ get_seed()

uint64_t gpmp::core::rndm::LCG::get_seed ( ) const

Definition at line 79 of file random.cpp.

79  {
80  return state;
81 }

◆ operator()()

uint64_t gpmp::core::rndm::LCG::operator() ( )

Definition at line 49 of file random.cpp.

49  {
50  // Ensure the state is non-negative
51  state &= 0x7FFFFFFFFFFFFFFFULL;
52 
53  // Linear Congruential Generator algorithm
55 
56  // Overflow handling
57  if (state < increment) {
58  // Overflow occurred, adjust the state
59  state += increment;
60  }
61 
62  return state;
63 }

◆ seed()

void gpmp::core::rndm::LCG::seed ( uint64_t  new_seed)

Definition at line 66 of file random.cpp.

66  {
67  state = new_seed;
68 }

Member Data Documentation

◆ increment

uint64_t gpmp::core::rndm::LCG::increment
private

Definition at line 108 of file random.hpp.

◆ multiplier

uint64_t gpmp::core::rndm::LCG::multiplier
private

Definition at line 107 of file random.hpp.

◆ state

uint64_t gpmp::core::rndm::LCG::state
private

Definition at line 106 of file random.hpp.


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