Chaste Release::3.1
RandomNumberGenerator.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, 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 <cmath>
00037 #include <ctime>
00038 #include <cstdlib>
00039 #include <cassert>
00040 #include <iostream>
00041 #include <vector>
00042 
00043 #include "RandomNumberGenerator.hpp"
00044 
00045 RandomNumberGenerator* RandomNumberGenerator::mpInstance = NULL;
00046 
00047 RandomNumberGenerator::RandomNumberGenerator()
00048     : mSeed(0),
00049       mTimesCalled(0),
00050       mWorkingMem1(0),
00051       mWorkingMem2(0),
00052       mWorkingMemW(0),
00053       mRandNum2(0),
00054       mUseLastNum(false)
00055 {
00056     assert(mpInstance == NULL); // Ensure correct serialization
00057     srandom(mSeed);
00058 }
00059 
00060 RandomNumberGenerator* RandomNumberGenerator::Instance()
00061 {
00062     if (mpInstance == NULL)
00063     {
00064         mpInstance = new RandomNumberGenerator();
00065     }
00066     return mpInstance;
00067 }
00068 
00069 void RandomNumberGenerator::Destroy()
00070 {
00071     if (mpInstance)
00072     {
00073         delete mpInstance;
00074         mpInstance = NULL;
00075     }
00076 }
00077 
00078 unsigned RandomNumberGenerator::randMod(unsigned base)
00079 {
00080     mTimesCalled++;
00081     return (random()%base);
00082 }
00083 
00084 double RandomNumberGenerator::ranf()
00085 {
00086     mTimesCalled++;
00087     return (double)random() / RAND_MAX;
00088 }
00089 
00090 double RandomNumberGenerator::NormalRandomDeviate(double mean, double sd)
00091 {
00092     return sd * StandardNormalRandomDeviate() + mean;
00093 }
00094 
00095 void RandomNumberGenerator::Reseed(int seed)
00096 {
00097     mSeed = seed;
00098     srandom(mSeed);
00099     mTimesCalled = 0;
00100 }
00101 
00102 void RandomNumberGenerator::Shuffle(unsigned num, std::vector<unsigned>& rValues)
00103 {
00104     rValues.resize(num);
00105     for (unsigned i=0; i<num; i++)
00106     {
00107         rValues[i] = i;
00108     }
00109 
00110     for (unsigned end=num-1; end>0; end--)
00111     {
00112         // Pick a random integer from {0,..,end}
00113         unsigned k = RandomNumberGenerator::Instance()->randMod(end+1);
00114         unsigned temp = rValues[end];
00115         rValues[end] = rValues[k];
00116         rValues[k] = temp;
00117     }
00118 }
00119 
00120 double  RandomNumberGenerator::StandardNormalRandomDeviate()
00121 {
00139     if (mUseLastNum) /* We use value generated at the previous call */
00140     {
00141         mUseLastNum = false;
00142         return mRandNum2;
00143     }
00144     else
00145     {
00146         mUseLastNum = true;
00147         do
00148         {
00149             mWorkingMem1 = 2.0 * this->ranf() - 1.0;
00150             mWorkingMem2 = 2.0 * this->ranf() - 1.0;
00151             mWorkingMemW = mWorkingMem1 * mWorkingMem1 + mWorkingMem2 * mWorkingMem2;
00152         }
00153         while ( mWorkingMemW >= 1.0 );
00154 
00155         mWorkingMemW = sqrt((-2.0*log(mWorkingMemW))/mWorkingMemW);
00156         mRandNum2 = mWorkingMem2*mWorkingMemW;
00157         return mWorkingMem1*mWorkingMemW;
00158     }
00159 }