RandomNumberGenerator.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "Exception.hpp"
00037 #include "RandomNumberGenerator.hpp"
00038 
00039 RandomNumberGenerator* RandomNumberGenerator::mpInstance = NULL;
00040 
00041 RandomNumberGenerator::RandomNumberGenerator()
00042     : mMersenneTwisterGenerator(0u),
00043       mGenerateUnitReal(mMersenneTwisterGenerator, boost::uniform_real<>()),
00044 #if BOOST_VERSION < 105600  //#2585
00045       mGenerateStandardNormal(mMersenneTwisterGenerator, boost::random::normal_distribution_v156<>(mMersenneTwisterGenerator, 0.0, 1.0))
00046 #else
00047       mGenerateStandardNormal(mMersenneTwisterGenerator, boost::normal_distribution<>(0.0, 1.0))
00048 #endif
00049 {
00050     assert(mpInstance == NULL); // Ensure correct serialization
00051 }
00052 
00053 RandomNumberGenerator* RandomNumberGenerator::Instance()
00054 {
00055     if (mpInstance == NULL)
00056     {
00057         mpInstance = new RandomNumberGenerator();
00058     }
00059     return mpInstance;
00060 }
00061 
00062 void RandomNumberGenerator::Destroy()
00063 {
00064     if (mpInstance)
00065     {
00066         delete mpInstance;
00067         mpInstance = NULL;
00068     }
00069 }
00070 
00071 unsigned RandomNumberGenerator::randMod(unsigned base)
00072 {
00073     assert(base > 0u);
00074     /*
00075      * The contents of this method are copied out of
00076      * boost/include/boost/random/uniform_smallint.hpp lines 235 - 255
00077      * as of v 1.48 (preserved at least as far as 1.51).
00078      * to make sure we get the same
00079      * result on earlier versions of boost.
00080      *
00081      * It was then simplified as we know '_min' is zero, '_max' is 'base-1u'
00082      * and all the types are unsigneds.
00083      */
00084 
00085 #if BOOST_VERSION < 103700
00086     unsigned base_range =(mMersenneTwisterGenerator.max)() - (mMersenneTwisterGenerator.min)();
00087     unsigned val = mMersenneTwisterGenerator() - (mMersenneTwisterGenerator.min)();
00088 #else
00089     // equivalent to (eng() - eng.min()) % (_max - _min + 1) + _min,
00090     // but guarantees no overflow.
00091     unsigned base_range =
00092         boost::random::detail::subtract<unsigned>()((mMersenneTwisterGenerator.max)(), (mMersenneTwisterGenerator.min)());
00093     unsigned val =
00094         boost::random::detail::subtract<unsigned>()(mMersenneTwisterGenerator(), (mMersenneTwisterGenerator.min)());
00095 #endif
00096 
00097     if (base - 1u >= base_range)
00098     {
00099         // This was in the original boost file for when '_min' is large, but here it is zero so
00100         // we shouldn't ever reach this.
00101         NEVER_REACHED;
00102         //return val;
00103     }
00104     else
00105     {
00106         return (val % base);
00107     }
00108 }
00109 
00110 double RandomNumberGenerator::ranf()
00111 {
00112     return mGenerateUnitReal();
00113 }
00114 
00115 double RandomNumberGenerator::StandardNormalRandomDeviate()
00116 {
00117     return mGenerateStandardNormal();
00118 }
00119 
00120 double RandomNumberGenerator::NormalRandomDeviate(double mean, double stdDev)
00121 {
00122     return stdDev * StandardNormalRandomDeviate() + mean;
00123 }
00124 
00125 double RandomNumberGenerator::GammaRandomDeviate(double shape, double scale)
00126 {
00127     boost::gamma_distribution<> gd(shape);
00128     boost::variate_generator<boost::mt19937& , boost::gamma_distribution<> > var_gamma(mMersenneTwisterGenerator, gd);
00129 
00130     return scale*var_gamma();
00131 }
00132 
00133 double RandomNumberGenerator::ExponentialRandomDeviate(double scale)
00134 {
00135     // make an exponential distribution
00136     boost::exponential_distribution<> ed(scale);
00137 
00138     // `merge' this distribution with our random number generator
00139     boost::variate_generator<boost::mt19937& , boost::exponential_distribution<> > var_exponential(mMersenneTwisterGenerator, ed);
00140 
00141     // return the random number
00142     return var_exponential();
00143 }
00144 
00145 void RandomNumberGenerator::Reseed(unsigned seed)
00146 {
00147     mMersenneTwisterGenerator.seed(seed);
00148 
00149     // Because this does some Box-Muller type thing it remembers if you don't reset it - see #2633
00150     mGenerateStandardNormal.distribution().reset();
00151 
00152     // Probably don't need to do this, but it probably is good practice!
00153     mGenerateUnitReal.distribution().reset();
00154 }
00155 
00156 void RandomNumberGenerator::Shuffle(unsigned num, std::vector<unsigned>& rValues)
00157 {
00158     rValues.resize(num);
00159     for (unsigned i=0; i<num; i++)
00160     {
00161         rValues[i] = i;
00162     }
00163 
00164     for (unsigned end=num-1; end>0; end--)
00165     {
00166         // Pick a random integer from {0,..,end}
00167         unsigned k = RandomNumberGenerator::Instance()->randMod(end+1);
00168         unsigned temp = rValues[end];
00169         rValues[end] = rValues[k];
00170         rValues[k] = temp;
00171     }
00172 }

Generated by  doxygen 1.6.2