Chaste  Release::2017.1
RandomNumberGenerator.hpp
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 #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>
51 #include "ChasteSerialization.hpp"
52 #include "SerializableSingleton.hpp"
53 
81 class RandomNumberGenerator : public SerializableSingleton<RandomNumberGenerator>
82 {
83 private:
85  boost::mt19937 mMersenneTwisterGenerator;
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
98 
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 
154 protected:
160 
161 public:
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_*/
unsigned randMod(unsigned base)
boost::variate_generator< boost::mt19937 &, boost::random::normal_distribution_v165<> > mGenerateStandardNormal
static RandomNumberGenerator * mpInstance
double NormalRandomDeviate(double mean, double stdDev)
boost::variate_generator< boost::mt19937 &, boost::uniform_real<> > mGenerateUnitReal
void Shuffle(std::vector< boost::shared_ptr< T > > &rValues)
void save(Archive &archive, const unsigned int version) const
boost::mt19937 mMersenneTwisterGenerator
static RandomNumberGenerator * Instance()
double GammaRandomDeviate(double shape, double scale)
void load(Archive &archive, const unsigned int version)
double ExponentialRandomDeviate(double scale)