RandomNumberGenerator.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2014, 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       mGenerateStandardNormal(mMersenneTwisterGenerator, boost::normal_distribution<>(0.0, 1.0))
00045 {
00046     assert(mpInstance == NULL); // Ensure correct serialization
00047 }
00048 
00049 RandomNumberGenerator* RandomNumberGenerator::Instance()
00050 {
00051     if (mpInstance == NULL)
00052     {
00053         mpInstance = new RandomNumberGenerator();
00054     }
00055     return mpInstance;
00056 }
00057 
00058 void RandomNumberGenerator::Destroy()
00059 {
00060     if (mpInstance)
00061     {
00062         delete mpInstance;
00063         mpInstance = NULL;
00064     }
00065 }
00066 
00067 unsigned RandomNumberGenerator::randMod(unsigned base)
00068 {
00069     assert(base > 0u);
00070     /*
00071      * The contents of this method are copied out of
00072      * boost/include/boost/random/uniform_smallint.hpp lines 235 - 255
00073      * as of v 1.48 (preserved at least as far as 1.51).
00074      * to make sure we get the same
00075      * result on earlier versions of boost.
00076      *
00077      * It was then simplified as we know '_min' is zero, '_max' is 'base-1u'
00078      * and all the types are unsigneds.
00079      */
00080 
00081 #if BOOST_VERSION < 103700
00082     unsigned base_range =(mMersenneTwisterGenerator.max)() - (mMersenneTwisterGenerator.min)();
00083     unsigned val = mMersenneTwisterGenerator() - (mMersenneTwisterGenerator.min)();
00084 #else
00085     // equivalent to (eng() - eng.min()) % (_max - _min + 1) + _min,
00086     // but guarantees no overflow.
00087     unsigned base_range =
00088         boost::random::detail::subtract<unsigned>()((mMersenneTwisterGenerator.max)(), (mMersenneTwisterGenerator.min)());
00089     unsigned val =
00090         boost::random::detail::subtract<unsigned>()(mMersenneTwisterGenerator(), (mMersenneTwisterGenerator.min)());
00091 #endif
00092 
00093     if (base - 1u >= base_range)
00094     {
00095         // This was in the original boost file for when '_min' is large, but here it is zero so
00096         // we shouldn't ever reach this.
00097         NEVER_REACHED;
00098         //return val;
00099     }
00100     else
00101     {
00102         return (val % base);
00103     }
00104 }
00105 
00106 double RandomNumberGenerator::ranf()
00107 {
00108     return mGenerateUnitReal();
00109 }
00110 
00111 double RandomNumberGenerator::StandardNormalRandomDeviate()
00112 {
00113     return mGenerateStandardNormal();
00114 }
00115 
00116 double RandomNumberGenerator::NormalRandomDeviate(double mean, double stdDev)
00117 {
00118     return stdDev * StandardNormalRandomDeviate() + mean;
00119 }
00120 
00121 double RandomNumberGenerator::GammaRandomDeviate(double shape, double scale)
00122 {
00123     boost::gamma_distribution<> gd(shape);
00124     boost::variate_generator<boost::mt19937& , boost::gamma_distribution<> > var_gamma(mMersenneTwisterGenerator, gd);
00125 
00126     return scale*var_gamma();
00127 }
00128 
00129 double RandomNumberGenerator::ExponentialRandomDeviate(double scale)
00130 {
00131     // make an exponential distribution
00132     boost::exponential_distribution<> ed(scale);
00133 
00134     // `merge' this distribution with our random number generator
00135     boost::variate_generator<boost::mt19937& , boost::exponential_distribution<> > var_exponential(mMersenneTwisterGenerator, ed);
00136 
00137     // return the random number
00138     return var_exponential();
00139 }
00140 
00141 void RandomNumberGenerator::Reseed(unsigned seed)
00142 {
00143     mMersenneTwisterGenerator.seed(seed);
00144 }
00145 
00146 void RandomNumberGenerator::Shuffle(unsigned num, std::vector<unsigned>& rValues)
00147 {
00148     rValues.resize(num);
00149     for (unsigned i=0; i<num; i++)
00150     {
00151         rValues[i] = i;
00152     }
00153 
00154     for (unsigned end=num-1; end>0; end--)
00155     {
00156         // Pick a random integer from {0,..,end}
00157         unsigned k = RandomNumberGenerator::Instance()->randMod(end+1);
00158         unsigned temp = rValues[end];
00159         rValues[end] = rValues[k];
00160         rValues[k] = temp;
00161     }
00162 }

Generated by  doxygen 1.6.2