00001 /****************************************************************************/ 00007 // 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 /****************************************************************************/ 00019 #ifndef RandHelper_h 00020 #define RandHelper_h 00021 00022 00023 // =========================================================================== 00024 // included modules 00025 // =========================================================================== 00026 #ifdef _MSC_VER 00027 #include <windows_config.h> 00028 #else 00029 #include <config.h> 00030 #endif 00031 00032 #include <vector> 00033 #include <foreign/mersenne/MersenneTwister.h> 00034 00035 00036 // =========================================================================== 00037 // class declarations 00038 // =========================================================================== 00039 class OptionsCont; 00040 00041 00042 // =========================================================================== 00043 // class definitions 00044 // =========================================================================== 00049 class RandHelper { 00050 public: 00052 static void insertRandOptions(); 00053 00055 static void initRandGlobal(); 00056 00058 static inline SUMOReal rand() { 00059 return (SUMOReal) RandHelper::myRandomNumberGenerator.randExc(); 00060 } 00061 00063 static inline SUMOReal rand(SUMOReal maxV) { 00064 return maxV * rand(); 00065 } 00066 00068 static inline SUMOReal rand(SUMOReal minV, SUMOReal maxV) { 00069 return minV + (maxV - minV) * rand(); 00070 } 00071 00073 static inline size_t rand(size_t maxV) { 00074 return (size_t) RandHelper::myRandomNumberGenerator.randInt((MTRand::uint32)(maxV-1)); 00075 } 00076 00078 static inline int rand(int maxV) { 00079 return (int) RandHelper::myRandomNumberGenerator.randInt((MTRand::uint32)(maxV-1)); 00080 } 00081 00083 static inline int rand(int minV, int maxV) { 00084 return minV + rand(maxV - minV); 00085 } 00086 00088 static inline SUMOReal randNorm(SUMOReal mean, SUMOReal variance) { 00089 return (SUMOReal) RandHelper::myRandomNumberGenerator.randNorm(mean, variance); 00090 } 00091 00093 template<class T> 00094 static inline T 00095 getRandomFrom(const std::vector<T> &v) { 00096 return v[rand(v.size())]; 00097 } 00098 00099 protected: 00101 static MTRand myRandomNumberGenerator; 00102 00103 }; 00104 00105 #endif 00106 00107 /****************************************************************************/ 00108
1.5.6