RandomNumberGenerator.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include <cmath>
00030 #include <ctime>
00031 #include <cstdlib>
00032 #include <iostream>
00033 #include <vector>
00034 
00035 #include "RandomNumberGenerator.hpp"
00036 
00037 RandomNumberGenerator* RandomNumberGenerator::mpInstance = NULL;
00038 
00039 RandomNumberGenerator::RandomNumberGenerator()
00040     : mSeed(0),
00041       mTimesCalled(0),
00042       mWorkingMem1(0),
00043       mWorkingMem2(0),
00044       mWorkingMemW(0),
00045       mRandNum2(0),
00046       mUseLastNum(false)
00047 {
00048     srandom(mSeed);
00049 }
00050 
00051 RandomNumberGenerator* RandomNumberGenerator::Instance()
00052 {
00053     if (mpInstance == NULL)
00054     {
00055         mpInstance = new RandomNumberGenerator();
00056     }
00057     return mpInstance;
00058 }
00059 
00060 void RandomNumberGenerator::Destroy()
00061 {
00062     if (mpInstance)
00063     {
00064         delete mpInstance;
00065         mpInstance = NULL;
00066     }
00067 }
00068 
00069 unsigned RandomNumberGenerator::randMod(unsigned base)
00070 {
00071     mTimesCalled++;
00072     return (random()%base);
00073 }
00074 
00075 double RandomNumberGenerator::ranf()
00076 {
00077     mTimesCalled++;
00078     return (double)random() / RAND_MAX;
00079 }
00080 
00081 double RandomNumberGenerator::NormalRandomDeviate(double mean, double sd)
00082 {
00083     return sd * StandardNormalRandomDeviate() + mean;
00084 }
00085 
00086 void RandomNumberGenerator::Reseed(int seed)
00087 {
00088     mSeed = seed;
00089     srandom(mSeed);
00090     mTimesCalled = 0;
00091 }
00092 
00093 void RandomNumberGenerator::Shuffle(unsigned num, std::vector<unsigned>& rValues)
00094 {
00095     rValues.resize(num);
00096     for (unsigned i=0; i<num; i++)
00097     {
00098         rValues[i] = i;
00099     }
00100 
00101     for (unsigned end=num-1; end>0; end--)
00102     {
00103         // Pick a random integer from {0,..,end}
00104         unsigned k = RandomNumberGenerator::Instance()->randMod(end+1);
00105         unsigned temp = rValues[end];
00106         rValues[end] = rValues[k];
00107         rValues[k] = temp;
00108     }
00109 }
00110 
00111 
00112 double  RandomNumberGenerator::StandardNormalRandomDeviate()
00113 {
00132     if (mUseLastNum) /* We use value generated at the previous call */
00133     {
00134         mUseLastNum = false;
00135         return mRandNum2;
00136     }
00137     else
00138     {
00139         mUseLastNum = true;
00140         do
00141         {
00142             mWorkingMem1 = 2.0 * this->ranf() - 1.0;
00143             mWorkingMem2 = 2.0 * this->ranf() - 1.0;
00144             mWorkingMemW = mWorkingMem1 * mWorkingMem1 + mWorkingMem2 * mWorkingMem2;
00145         }
00146         while ( mWorkingMemW >= 1.0 );
00147 
00148         mWorkingMemW = sqrt((-2.0*log(mWorkingMemW))/mWorkingMemW);
00149         mRandNum2 = mWorkingMem2*mWorkingMemW;
00150         return mWorkingMem1*mWorkingMemW;
00151     }
00152 }
00153 
00154 
00155 
00156 

Generated on Tue May 31 14:31:41 2011 for Chaste by  doxygen 1.5.5