Chaste  Release::3.4
RandomNumberGenerator.hpp
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 #ifndef RANDOMNUMBERGENERATORS_HPP_
37 #define RANDOMNUMBERGENERATORS_HPP_
38 
39 #include <sstream>
40 #include <boost/shared_ptr.hpp>
41 #include <boost/version.hpp>
42 
43 
44 #if BOOST_VERSION < 105600
45 // Forward compatibility with Boost 1.56 onwards
47 #endif
48 
49 #include <boost/random.hpp>
50 
51 #include "ChasteSerialization.hpp"
52 #include "SerializableSingleton.hpp"
53 #include <boost/serialization/split_member.hpp>
54 
59 class RandomNumberGenerator : public SerializableSingleton<RandomNumberGenerator>
60 {
61 private:
63  boost::mt19937 mMersenneTwisterGenerator;
64 
65  // If you add any more generators below, then remember to add lines for them in the Reseed() method too.
66 
68  boost::variate_generator<boost::mt19937& , boost::uniform_real<> > mGenerateUnitReal;
69 
71 #if BOOST_VERSION < 105600 //#2585
72  boost::variate_generator<boost::mt19937& , boost::random::normal_distribution_v156<> > mGenerateStandardNormal;
73 #else
74  boost::variate_generator<boost::mt19937& , boost::normal_distribution<> > mGenerateStandardNormal;
75 #endif
76 
78 
79  friend class boost::serialization::access;
89  template<class Archive>
90  void save(Archive & archive, const unsigned int version) const
91  {
92  std::stringstream rng_internals;
93  rng_internals << mMersenneTwisterGenerator;
94  std::string rng_internals_string = rng_internals.str();
95  archive & rng_internals_string;
96 
97  std::stringstream normal_internals;
98 #if BOOST_VERSION < 105600 //#2585
99  const boost::random::normal_distribution_v156<>& r_normal_dist = mGenerateStandardNormal.distribution();
100 #else
101  const boost::normal_distribution<>& r_normal_dist = mGenerateStandardNormal.distribution();
102 #endif
103  normal_internals << r_normal_dist;
104  std::string normal_internals_string = normal_internals.str();
105  archive & normal_internals_string;
106  }
107 
117  template<class Archive>
118  void load(Archive & archive, const unsigned int version)
119  {
120  std::string rng_internals_string;
121  archive & rng_internals_string;
122  std::stringstream rng_internals(rng_internals_string);
123  rng_internals >> mMersenneTwisterGenerator;
124 
125  std::string normal_internals_string;
126  archive & normal_internals_string;
127  std::stringstream normal_internals(normal_internals_string);
128  normal_internals >> mGenerateStandardNormal.distribution();
129  }
130  BOOST_SERIALIZATION_SPLIT_MEMBER()
131 
132 protected:
133 
139 
140 public:
141 
148 
156  double NormalRandomDeviate(double mean, double stdDev);
157 
161  double ranf();
162 
169  double GammaRandomDeviate(double shape, double scale);
170 
176  double ExponentialRandomDeviate(double scale);
177 
184  unsigned randMod(unsigned base);
185 
193  template <class T>
194  void Shuffle(std::vector<boost::shared_ptr<T> >& rValues)
195  {
196  unsigned num = rValues.size();
197  if (num == 0)
198  {
199  return;
200  }
201  for (unsigned end=num-1; end>0; end--)
202  {
203  // Pick a random integer from {0,..,end}
204  unsigned k = RandomNumberGenerator::Instance()->randMod(end+1);
205  boost::shared_ptr<T> temp = rValues[end];
206  rValues[end] = rValues[k];
207  rValues[k] = temp;
208  }
209  }
210 
220  void Shuffle(unsigned num, std::vector<unsigned>& rValues);
221 
227 
234  static void Destroy();
235 
241  void Reseed(unsigned seed);
242 };
243 
244 #endif /*RANDOMNUMBERGENERATORS_HPP_*/
boost::variate_generator< boost::mt19937 &, boost::random::normal_distribution_v156<> > mGenerateStandardNormal
unsigned randMod(unsigned base)
static RandomNumberGenerator * mpInstance
double NormalRandomDeviate(double mean, double stdDev)
void Shuffle(std::vector< boost::shared_ptr< T > > &rValues)
boost::variate_generator< boost::mt19937 &, boost::uniform_real<> > mGenerateUnitReal
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)