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