Chaste  Release::2017.1
RandomNumberGenerator.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF 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 
60 {
61  if (mpInstance == nullptr)
62  {
64  }
65  return mpInstance;
66 }
67 
69 {
70  if (mpInstance)
71  {
72  delete mpInstance;
73  mpInstance = nullptr;
74  }
75 }
76 
77 unsigned 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 
115 {
116  return mGenerateStandardNormal();
117 }
118 
119 double RandomNumberGenerator::NormalRandomDeviate(double mean, double stdDev)
120 {
121  return stdDev * StandardNormalRandomDeviate() + mean;
122 }
123 
124 double 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 
153 void RandomNumberGenerator::Reseed(unsigned seed)
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 
164 void 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 }
unsigned randMod(unsigned base)
boost::variate_generator< boost::mt19937 &, boost::random::normal_distribution_v165<> > mGenerateStandardNormal
static RandomNumberGenerator * mpInstance
double NormalRandomDeviate(double mean, double stdDev)
#define NEVER_REACHED
Definition: Exception.hpp:206
boost::variate_generator< boost::mt19937 &, boost::uniform_real<> > mGenerateUnitReal
void Shuffle(std::vector< boost::shared_ptr< T > > &rValues)
boost::mt19937 mMersenneTwisterGenerator
static RandomNumberGenerator * Instance()
double GammaRandomDeviate(double shape, double scale)
double ExponentialRandomDeviate(double scale)