Mantid
Loading...
Searching...
No Matches
MersenneTwister.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//------------------------------------------------------------------------------
8// Includes
9//------------------------------------------------------------------------------
11
13
14#include <Poco/Timestamp.h>
15
16namespace Mantid::Kernel {
17
18//------------------------------------------------------------------------------
19// Public member functions
20//------------------------------------------------------------------------------
21
26MersenneTwister::MersenneTwister(const size_t seedValue) : MersenneTwister(seedValue, 0.0, 1.0) {}
27
33
39MersenneTwister::MersenneTwister(const double start, const double end)
40 : MersenneTwister(Poco::Timestamp().epochMicroseconds(), start, end) {}
41
48MersenneTwister::MersenneTwister(const size_t seedValue, const double start, const double end)
49 : m_engine(), uniformRealDistribution(start, end), m_start(start), m_end(end), m_seed(), m_savedEngine() {
50 setSeed(seedValue);
51}
52
57void MersenneTwister::setSeed(const size_t seedValue) {
58 m_seed = static_cast<std::mt19937::result_type>(seedValue);
59 m_engine.seed(m_seed);
60 m_savedEngine = std::make_unique<std::mt19937>(m_engine);
61}
62
68void MersenneTwister::setRange(const double start, const double end) {
69 m_start = start;
70 m_end = end;
71 uniformRealDistribution = std::uniform_real_distribution<double>(start, end);
72}
73
81int MersenneTwister::nextInt(int start, int end) {
83}
84
89
91void MersenneTwister::save() { m_savedEngine = std::make_unique<std::mt19937>(m_engine); }
92
96 // Copy saved to current, still distinct objects so that another restore still
97 // brings us
98 // back to the originally saved point
99 if (m_savedEngine) {
100 m_engine = std::mt19937(*m_savedEngine);
101 } else {
102 restart();
103 }
104}
105} // namespace Mantid::Kernel
This implements the the Mersenne Twister 19937 pseudo-random number generator algorithm as a specialz...
void restore() override
Restores the generator to the last saved point, or the beginning if nothing has been saved.
std::uniform_real_distribution< double > uniformRealDistribution
Uniform Real distribution.
double m_end
Maximum in range.
MersenneTwister()
Construct the generator using time stamp for the initial seed.
void restart() override
Resets the generator.
void setSeed(const size_t seedValue) override
Set the random number seed.
void setRange(const double start, const double end) override
Sets the range of the subsequent calls to next.
double m_start
Minimum in range.
std::mt19937::result_type m_seed
The current seed.
std::mt19937 m_engine
The engine.
std::unique_ptr< std::mt19937 > m_savedEngine
A generator that will take the value when save is requested.
void save() override
Saves the current state of the generator.
int nextInt(int start, int end) override
Return the next integer in the sequence within the given range.
Definition: Algorithm.h:30