Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
RandomNumberGenerator.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#ifndef RANDOMNUMBERGENERATORS_HPP_
37#define RANDOMNUMBERGENERATORS_HPP_
38
39#include <boost/shared_ptr.hpp>
40#include <boost/version.hpp>
41#include <sstream>
42
43#if BOOST_VERSION < 106400
44// Forward compatibility with Boost 1.64 onwards
46#endif
47
48#include <boost/random.hpp>
49
50#include <boost/serialization/split_member.hpp>
52#include "SerializableSingleton.hpp"
53
81class RandomNumberGenerator : public SerializableSingleton<RandomNumberGenerator>
82{
83private:
86
87 // If you add any more generators below, then remember to add lines for them in the Reseed() method too.
88
90 boost::variate_generator<boost::mt19937&, boost::uniform_real<> > mGenerateUnitReal;
91
93#if BOOST_VERSION < 106400 // #2585 and #2893
94 boost::variate_generator<boost::mt19937&, boost::random::normal_distribution_v165<> > mGenerateStandardNormal;
95#else
96 boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > mGenerateStandardNormal;
97#endif
100
101 friend class boost::serialization::access;
111 template <class Archive>
112 void save(Archive& archive, const unsigned int version) const
113 {
114 std::stringstream rng_internals;
115 rng_internals << mMersenneTwisterGenerator;
116 std::string rng_internals_string = rng_internals.str();
117 archive& rng_internals_string;
118
119 std::stringstream normal_internals;
120#if BOOST_VERSION < 106400 // #2585 and #2893
121 const boost::random::normal_distribution_v165<>& r_normal_dist = mGenerateStandardNormal.distribution();
122#else
123 const boost::normal_distribution<>& r_normal_dist = mGenerateStandardNormal.distribution();
124#endif
125 normal_internals << r_normal_dist;
126 std::string normal_internals_string = normal_internals.str();
127 archive& normal_internals_string;
128 }
129
139 template <class Archive>
140 void load(Archive& archive, const unsigned int version)
141 {
142 std::string rng_internals_string;
143 archive& rng_internals_string;
144 std::stringstream rng_internals(rng_internals_string);
145 rng_internals >> mMersenneTwisterGenerator;
146
147 std::string normal_internals_string;
148 archive& normal_internals_string;
149 std::stringstream normal_internals(normal_internals_string);
150 normal_internals >> mGenerateStandardNormal.distribution();
151 }
152 BOOST_SERIALIZATION_SPLIT_MEMBER()
153
154protected:
160
161public:
168
176 double NormalRandomDeviate(double mean, double stdDev);
177
181 double ranf();
182
189 double GammaRandomDeviate(double shape, double scale);
190
196 double ExponentialRandomDeviate(double scale);
197
204 unsigned randMod(unsigned base);
205
213 template <class T>
214 void Shuffle(std::vector<boost::shared_ptr<T> >& rValues)
215 {
216 unsigned num = rValues.size();
217 if (num == 0)
218 {
219 return;
220 }
221 for (unsigned end = num - 1; end > 0; end--)
222 {
223 // Pick a random integer from {0,..,end}
224 unsigned k = RandomNumberGenerator::Instance()->randMod(end + 1);
225 boost::shared_ptr<T> temp = rValues[end];
226 rValues[end] = rValues[k];
227 rValues[k] = temp;
228 }
229 }
230
240 void Shuffle(unsigned num, std::vector<unsigned>& rValues);
241
247
254 static void Destroy();
255
261 void Reseed(unsigned seed);
262};
263
264#endif /*RANDOMNUMBERGENERATORS_HPP_*/
boost::variate_generator< boost::mt19937 &, boost::uniform_real<> > mGenerateUnitReal
double NormalRandomDeviate(double mean, double stdDev)
void save(Archive &archive, const unsigned int version) const
double GammaRandomDeviate(double shape, double scale)
static RandomNumberGenerator * Instance()
void load(Archive &archive, const unsigned int version)
void Shuffle(std::vector< boost::shared_ptr< T > > &rValues)
unsigned randMod(unsigned base)
double ExponentialRandomDeviate(double scale)
static RandomNumberGenerator * mpInstance
boost::mt19937 mMersenneTwisterGenerator
boost::variate_generator< boost::mt19937 &, boost::random::normal_distribution_v165<> > mGenerateStandardNormal