Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
RandomNumberGenerator.cpp
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#include "RandomNumberGenerator.hpp"
37#include "Exception.hpp"
38
39#if BOOST_VERSION < 106400 // #2893
40// Forward compatibility with Boost 1.64 onwards
43#endif
44
46
48 : mMersenneTwisterGenerator(0u),
49 mGenerateUnitReal(mMersenneTwisterGenerator, boost::uniform_real<>()),
50#if BOOST_VERSION < 106400 // #2585 and #2893
51 mGenerateStandardNormal(mMersenneTwisterGenerator, boost::random::normal_distribution_v165<>(0.0, 1.0))
52#else
53 mGenerateStandardNormal(mMersenneTwisterGenerator, boost::normal_distribution<>(0.0, 1.0))
54#endif
55{
56 assert(mpInstance == nullptr); // Ensure correct serialization
57}
58
67
69{
70 if (mpInstance)
71 {
72 delete mpInstance;
73 mpInstance = nullptr;
74 }
75}
76
77unsigned RandomNumberGenerator::randMod(unsigned base)
78{
79 assert(base > 0u);
80 /*
81 * The contents of this method are copied out of
82 * boost/include/boost/random/uniform_smallint.hpp lines 235 - 255
83 * as of v 1.48 (preserved at least as far as 1.51).
84 * to make sure we get the same
85 * result on earlier versions of boost.
86 *
87 * It was then simplified as we know '_min' is zero, '_max' is 'base-1u'
88 * and all the types are unsigneds.
89 */
90
91 // equivalent to (eng() - eng.min()) % (_max - _min + 1) + _min,
92 // but guarantees no overflow.
93 unsigned base_range = boost::random::detail::subtract<unsigned>()((mMersenneTwisterGenerator.max)(), (mMersenneTwisterGenerator.min)());
94 unsigned val = boost::random::detail::subtract<unsigned>()(mMersenneTwisterGenerator(), (mMersenneTwisterGenerator.min)());
95
96 if (base - 1u >= base_range)
97 {
98 // This was in the original boost file for when '_min' is large, but here it is zero so
99 // we shouldn't ever reach this.
101 //return val;
102 }
103 else
104 {
105 return (val % base);
106 }
107}
108
110{
111 return mGenerateUnitReal();
112}
113
118
119double RandomNumberGenerator::NormalRandomDeviate(double mean, double stdDev)
120{
121 return stdDev * StandardNormalRandomDeviate() + mean;
122}
123
124double RandomNumberGenerator::GammaRandomDeviate(double shape, double scale)
125{
126// make a gamma distribution and `merge' this distribution with our random number generator
127
128#if BOOST_VERSION < 106400
130 boost::variate_generator<boost::mt19937&, boost::random::gamma_distribution_v165<> > var_gamma(mMersenneTwisterGenerator, gd);
131#else
132 boost::gamma_distribution<> gd(shape);
133 boost::variate_generator<boost::mt19937&, boost::gamma_distribution<> > var_gamma(mMersenneTwisterGenerator, gd);
134#endif
135 return scale * var_gamma();
136}
137
139{
140// make an exponential distribution and `merge' this distribution with our random number generator
141
142#if BOOST_VERSION < 106400
144 boost::variate_generator<boost::mt19937&, boost::random::exponential_distribution_v165<> > var_exponential(mMersenneTwisterGenerator, ed);
145#else
146 boost::exponential_distribution<> ed(scale);
147 boost::variate_generator<boost::mt19937&, boost::exponential_distribution<> > var_exponential(mMersenneTwisterGenerator, ed);
148#endif
149 // return the random number
150 return var_exponential();
151}
152
154{
155 mMersenneTwisterGenerator.seed(seed);
156
157 // Because this does some Box-Muller type thing it remembers if you don't reset it - see #2633
158 mGenerateStandardNormal.distribution().reset();
159
160 // Probably don't need to do this, but it probably is good practice!
161 mGenerateUnitReal.distribution().reset();
162}
163
164void RandomNumberGenerator::Shuffle(unsigned num, std::vector<unsigned>& rValues)
165{
166 rValues.resize(num);
167 for (unsigned i = 0; i < num; i++)
168 {
169 rValues[i] = i;
170 }
171
172 for (unsigned end = num - 1; end > 0; end--)
173 {
174 // Pick a random integer from {0,..,end}
175 unsigned k = RandomNumberGenerator::Instance()->randMod(end + 1);
176 unsigned temp = rValues[end];
177 rValues[end] = rValues[k];
178 rValues[k] = temp;
179 }
180}
#define NEVER_REACHED
boost::variate_generator< boost::mt19937 &, boost::uniform_real<> > mGenerateUnitReal
double NormalRandomDeviate(double mean, double stdDev)
double GammaRandomDeviate(double shape, double scale)
static RandomNumberGenerator * Instance()
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