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 <cassert> 00033 #include <iostream> 00034 #include <vector> 00035 00036 #include "RandomNumberGenerator.hpp" 00037 00038 RandomNumberGenerator* RandomNumberGenerator::mpInstance = NULL; 00039 00040 RandomNumberGenerator::RandomNumberGenerator() 00041 : mSeed(0), 00042 mTimesCalled(0), 00043 mWorkingMem1(0), 00044 mWorkingMem2(0), 00045 mWorkingMemW(0), 00046 mRandNum2(0), 00047 mUseLastNum(false) 00048 { 00049 assert(mpInstance == NULL); // Ensure correct serialization 00050 srandom(mSeed); 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 mTimesCalled++; 00074 return (random()%base); 00075 } 00076 00077 double RandomNumberGenerator::ranf() 00078 { 00079 mTimesCalled++; 00080 return (double)random() / RAND_MAX; 00081 } 00082 00083 double RandomNumberGenerator::NormalRandomDeviate(double mean, double sd) 00084 { 00085 return sd * StandardNormalRandomDeviate() + mean; 00086 } 00087 00088 void RandomNumberGenerator::Reseed(int seed) 00089 { 00090 mSeed = seed; 00091 srandom(mSeed); 00092 mTimesCalled = 0; 00093 } 00094 00095 void RandomNumberGenerator::Shuffle(unsigned num, std::vector<unsigned>& rValues) 00096 { 00097 rValues.resize(num); 00098 for (unsigned i=0; i<num; i++) 00099 { 00100 rValues[i] = i; 00101 } 00102 00103 for (unsigned end=num-1; end>0; end--) 00104 { 00105 // Pick a random integer from {0,..,end} 00106 unsigned k = RandomNumberGenerator::Instance()->randMod(end+1); 00107 unsigned temp = rValues[end]; 00108 rValues[end] = rValues[k]; 00109 rValues[k] = temp; 00110 } 00111 } 00112 00113 double RandomNumberGenerator::StandardNormalRandomDeviate() 00114 { 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 }