QMCPACK
StdRandom.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
9 //
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 /** @file
13  * A minimally functional wrapper for the since c++11 <random>
14  */
15 #ifndef QMCPLUSPLUS_STDRAND_H
16 #define QMCPLUSPLUS_STDRAND_H
17 
18 #include "RandomBase.h"
19 
20 #include <vector>
21 #include <random>
22 #include <sstream>
23 #include <iterator>
24 
25 namespace qmcplusplus
26 {
27 
28 /** generating real type random numbers [min, max) in a uniform distribution.
29  * This is intended to match the behavior of random::uniform_real_distribution in the boost libraries
30  * Its behavor is different from std::uniform_real_distribution which uses std::generate_canonical
31  * to generate enough entropy according to the precision of T.
32  */
33 template<typename T>
35 {
36 public:
37  using result_type = T;
38  static_assert(std::is_floating_point_v<T>);
39 
40  uniform_real_distribution_as_boost(T min = T(0.0), T max = T(1.0)) : min_(min), max_(max) {}
41  ///Generating functions.
42  template<typename RNG>
44  {
45  return static_cast<result_type>(eng() - eng.min()) / (static_cast<result_type>(eng.max() - eng.min()) + 1) *
46  (max_ - min_) + min_;
47  }
48 
49 private:
50  T min_;
51  T max_;
52 };
53 
54 template<typename T>
55 class StdRandom : public RandomBase<T>
56 {
57 public:
58  using Engine = std::mt19937;
61 
62  StdRandom(uint_type iseed = 911);
63 
64  void init(int iseed_in) override
65  {
66  uint_type baseSeed = iseed_in;
67  engine.seed(baseSeed);
68  }
69 
70  void seed(uint_type aseed) override { engine.seed(aseed); }
71  result_type operator()() override;
72  void write(std::ostream& rout) const override { rout << engine; }
73  void read(std::istream& rin) override { rin >> engine; }
74  size_t state_size() const override { return stream_state_size; }
75 
76  void load(const std::vector<uint_type>& newstate) override;
77  void save(std::vector<uint_type>& curstate) const override;
78  std::unique_ptr<RandomBase<T>> makeClone() const override { return std::make_unique<StdRandom<T>>(*this); }
79 
80  // Non const allows use of default copy constructor
81  std::string ClassName{"StdRand"};
82  std::string EngineName{"std::mt19937"};
83 
84 private:
85  ///random number generator [0,1)
88  /// the number count of streaming states. Must match read/write/load/save
89  std::size_t stream_state_size;
90 };
91 
92 } // namespace qmcplusplus
93 
94 #endif
uniform_real_distribution_as_boost(T min=T(0.0), T max=T(1.0))
Definition: StdRandom.h:40
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
std::unique_ptr< RandomBase< T > > makeClone() const override
Definition: StdRandom.h:78
void read(std::istream &rin) override
Definition: StdRandom.h:73
void write(std::ostream &rout) const override
Definition: StdRandom.h:72
T min(T a, T b)
std::string ClassName
Definition: StdRandom.h:81
result_type operator()(RNG &eng)
Generating functions.
Definition: StdRandom.h:43
generating real type random numbers [min, max) in a uniform distribution.
Definition: StdRandom.h:34
result_type operator()() override
Definition: StdRandom.cpp:49
std::size_t stream_state_size
the number count of streaming states. Must match read/write/load/save
Definition: StdRandom.h:89
void load(const std::vector< uint_type > &newstate) override
Definition: StdRandom.cpp:32
void save(std::vector< uint_type > &curstate) const override
Definition: StdRandom.cpp:40
typename RandomBase< T >::result_type result_type
Definition: StdRandom.h:59
size_t state_size() const override
Definition: StdRandom.h:74
void init(int iseed_in) override
Definition: StdRandom.h:64
void seed(uint_type aseed) override
Definition: StdRandom.h:70
StdRandom(uint_type iseed=911)
Definition: StdRandom.cpp:17
uniform_real_distribution_as_boost< T > distribution
random number generator [0,1)
Definition: StdRandom.h:86
std::string EngineName
Definition: StdRandom.h:82