Quantum++  v1.0-rc2
A modern C++11 quantum computing library
random_devices.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2017 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef CLASSES_RANDOM_DEVICES_H_
28 #define CLASSES_RANDOM_DEVICES_H_
29 
30 namespace qpp
31 {
45 class RandomDevices final : public internal::Singleton<RandomDevices> //
46 // Singleton
47 {
49 
50  std::random_device rd_;
51  std::mt19937 prng_;
52 public:
57  std::mt19937& get_prng()
58  {
59  return prng_;
60  }
61 
67  std::istream& load(std::istream& is)
68  {
69  return is >> prng_;
70  }
71 
77  std::ostream& save(std::ostream& os) const
78  {
79  return os << prng_;
80  }
81 
82 private:
86  RandomDevices() : rd_{}, prng_{rd_()}
87  {
88  }
89 
93  ~RandomDevices() = default;
94 }; /* class RandomDevices */
95 
96 } /* namespace qpp */
97 
98 #endif /* CLASSES_RANDOM_DEVICES_H_ */
std::mt19937 & get_prng()
Returns a reference to the internal PRNG object.
Definition: random_devices.h:57
Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurr...
Definition: singleton.h:76
std::ostream & save(std::ostream &os) const
Saves the state of the PRNG to an output stream.
Definition: random_devices.h:77
Quantum++ main namespace.
Definition: codes.h:30
RandomDevices()
Initializes and seeds the random number generators.
Definition: random_devices.h:86
Singeleton class that manages the source of randomness in the library.
Definition: random_devices.h:45
std::random_device rd_
used to seed std::mt19937 prng_
Definition: random_devices.h:50
std::istream & load(std::istream &is)
Loads the state of the PRNG from an input stream.
Definition: random_devices.h:67
std::mt19937 prng_
Mersenne twister random number generator.
Definition: random_devices.h:51
~RandomDevices()=default
Default destructor.