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