QMCPACK
StdRandom.cpp
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) 2023 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 // Steven Hahn, hahnse@ornl.gov, Oak Ridge National Laboratory
10 //
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 #include "StdRandom.h"
14 namespace qmcplusplus
15 {
16 template<typename T>
17 StdRandom<T>::StdRandom(uint_type iseed) : 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 }
30 
31 template<typename T>
32 void StdRandom<T>::load(const std::vector<uint_type>& newstate)
33 {
34  std::stringstream otemp;
35  std::copy(newstate.begin(), newstate.end(), std::ostream_iterator<uint_type>(otemp, " "));
36  otemp >> engine;
37 }
38 
39 template<typename T>
40 void StdRandom<T>::save(std::vector<uint_type>& curstate) const
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 }
47 
48 template<typename T>
50 {
51  return distribution(engine);
52 }
53 
54 template class StdRandom<double>;
55 template class StdRandom<float>;
56 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
A minimally functional wrapper for the since c++11 <random>
typename RandomBase< T >::result_type result_type
Definition: StdRandom.h:59
StdRandom(uint_type iseed=911)
Definition: StdRandom.cpp:17