QMCPACK
StdRandom< T > Class Template Reference
+ Inheritance diagram for StdRandom< T >:
+ Collaboration diagram for StdRandom< T >:

Public Types

using Engine = std::mt19937
 
using result_type = typename RandomBase< T >::result_type
 
using uint_type = typename RandomBase< T >::uint_type
 
- Public Types inherited from RandomBase< T >
using result_type = T
 
using uint_type = uint_fast32_t
 

Public Member Functions

 StdRandom (uint_type iseed=911)
 
void init (int iseed_in) override
 
void seed (uint_type aseed) override
 
result_type operator() () override
 
void write (std::ostream &rout) const override
 
void read (std::istream &rin) override
 
size_t state_size () const override
 
void load (const std::vector< uint_type > &newstate) override
 
void save (std::vector< uint_type > &curstate) const override
 
std::unique_ptr< RandomBase< T > > makeClone () const override
 
- Public Member Functions inherited from RandomBase< T >
virtual ~RandomBase ()=default
 

Public Attributes

std::string ClassName {"StdRand"}
 
std::string EngineName {"std::mt19937"}
 

Private Attributes

uniform_real_distribution_as_boost< T > distribution
 random number generator [0,1) More...
 
Engine engine
 
std::size_t stream_state_size
 the number count of streaming states. Must match read/write/load/save More...
 

Detailed Description

template<typename T>
class qmcplusplus::StdRandom< T >

Definition at line 55 of file StdRandom.h.

Member Typedef Documentation

◆ Engine

using Engine = std::mt19937

Definition at line 58 of file StdRandom.h.

◆ result_type

using result_type = typename RandomBase<T>::result_type

Definition at line 59 of file StdRandom.h.

◆ uint_type

using uint_type = typename RandomBase<T>::uint_type

Definition at line 60 of file StdRandom.h.

Constructor & Destructor Documentation

◆ StdRandom()

StdRandom ( uint_type  iseed = 911)

Definition at line 17 of file StdRandom.cpp.

17  : engine(iseed)
18 {
19  static_assert(std::is_same_v<Engine::result_type, uint_type>);
20  // Although MT19937 needs only 624 numbers to hold the state, C++ standard libraries may choose different
21  // ways to represent the state. libc++ uses 624 numbers but libstdc++ uses 625 numbers. The additional
22  // number is an index to the 624 numbers. So we will just count and store the number.
23  std::vector<uint_type> state;
24  state.reserve(625); // the magic number is chosen based on libstdc++ using 625 numbers while libc++ uses 624
25  std::stringstream otemp;
26  otemp << engine;
27  std::copy(std::istream_iterator<uint_type>(otemp), std::istream_iterator<uint_type>(), std::back_inserter(state));
28  stream_state_size = state.size();
29 }
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
std::size_t stream_state_size
the number count of streaming states. Must match read/write/load/save
Definition: StdRandom.h:89

Member Function Documentation

◆ init()

void init ( int  iseed_in)
inlineoverridevirtual

Implements RandomBase< T >.

Definition at line 64 of file StdRandom.h.

Referenced by qmcplusplus::TEST_CASE().

65  {
66  uint_type baseSeed = iseed_in;
67  engine.seed(baseSeed);
68  }
typename RandomBase< T >::uint_type uint_type
Definition: StdRandom.h:60

◆ load()

void load ( const std::vector< uint_type > &  newstate)
overridevirtual

Implements RandomBase< T >.

Definition at line 32 of file StdRandom.cpp.

33 {
34  std::stringstream otemp;
35  std::copy(newstate.begin(), newstate.end(), std::ostream_iterator<uint_type>(otemp, " "));
36  otemp >> engine;
37 }
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639

◆ makeClone()

std::unique_ptr<RandomBase<T> > makeClone ( ) const
inlineoverridevirtual

Implements RandomBase< T >.

Definition at line 78 of file StdRandom.h.

78 { return std::make_unique<StdRandom<T>>(*this); }

◆ operator()()

StdRandom< T >::result_type operator() ( )
overridevirtual

Implements RandomBase< T >.

Definition at line 49 of file StdRandom.cpp.

50 {
51  return distribution(engine);
52 }
uniform_real_distribution_as_boost< T > distribution
random number generator [0,1)
Definition: StdRandom.h:86

◆ read()

void read ( std::istream &  rin)
inlineoverridevirtual

Implements RandomBase< T >.

Definition at line 73 of file StdRandom.h.

73 { rin >> engine; }

◆ save()

void save ( std::vector< uint_type > &  curstate) const
overridevirtual

Implements RandomBase< T >.

Definition at line 40 of file StdRandom.cpp.

41 {
42  curstate.clear();
43  std::stringstream otemp;
44  otemp << engine;
45  std::copy(std::istream_iterator<uint_type>(otemp), std::istream_iterator<uint_type>(), std::back_inserter(curstate));
46 }
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639

◆ seed()

void seed ( uint_type  aseed)
inlineoverridevirtual

Implements RandomBase< T >.

Definition at line 70 of file StdRandom.h.

70 { engine.seed(aseed); }

◆ state_size()

size_t state_size ( ) const
inlineoverridevirtual

Implements RandomBase< T >.

Definition at line 74 of file StdRandom.h.

74 { return stream_state_size; }
std::size_t stream_state_size
the number count of streaming states. Must match read/write/load/save
Definition: StdRandom.h:89

◆ write()

void write ( std::ostream &  rout) const
inlineoverridevirtual

Implements RandomBase< T >.

Definition at line 72 of file StdRandom.h.

72 { rout << engine; }

Member Data Documentation

◆ ClassName

std::string ClassName {"StdRand"}

Definition at line 81 of file StdRandom.h.

◆ distribution

uniform_real_distribution_as_boost<T> distribution
private

random number generator [0,1)

Definition at line 86 of file StdRandom.h.

◆ engine

◆ EngineName

std::string EngineName {"std::mt19937"}

Definition at line 82 of file StdRandom.h.

◆ stream_state_size

std::size_t stream_state_size
private

the number count of streaming states. Must match read/write/load/save

Definition at line 89 of file StdRandom.h.

Referenced by StdRandom< RngValueType< T > >::state_size().


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