Changeset 10813 for code/branches/cpp11_v2/src
- Timestamp:
- Nov 17, 2015, 6:28:25 PM (9 years ago)
- Location:
- code/branches/cpp11_v2/src/libraries
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/cpp11_v2/src/libraries/core/CoreConfig.cc
r10765 r10813 35 35 #include "core/Language.h" 36 36 #include "core/ApplicationPaths.h" 37 38 #include <random> 37 39 38 40 namespace orxonox … … 129 131 if (!bInitialized && this->bInitRandomNumberGenerator_) 130 132 { 131 srand(static_cast<unsigned int>(time(0))); 133 std::random_device rnddev; 134 rndseed(rnddev()); 135 //Keep the old seeding around because people will probably still use the old functions 136 srand(rnddev()); 132 137 rand(); 133 138 bInitialized = true; -
code/branches/cpp11_v2/src/libraries/util/Math.cc
r10630 r10813 371 371 } 372 372 373 374 namespace detail 375 { 376 std::mt19937 rngen; 377 } 378 373 379 /** 374 380 @brief Returns a unique number. This function will never return the same value twice. -
code/branches/cpp11_v2/src/libraries/util/Math.h
r10768 r10813 46 46 #include <cmath> 47 47 #include <cstdlib> 48 #include <random> 48 49 49 50 #include <OgreMath.h> … … 252 253 } 253 254 255 namespace detail 256 { 257 /** 258 Random number generator used for the functions below. Marked extern to only have one global instance. 259 */ 260 _UtilExport extern std::mt19937 rngen; 261 } 262 263 /** 264 @brief Seeds the random number generator used for the functions below. 265 */ 266 inline void rndseed(unsigned int seed) 267 { 268 detail::rngen.seed(seed); 269 } 270 271 /** 272 @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>. 273 @param min The minimum 274 @param max The maximum 275 */ 276 inline float rnd(float min, float max) 277 { 278 std::uniform_real_distribution<float> dist(min, max); 279 return dist(detail::rngen); 280 } 281 254 282 /** 255 283 @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>. … … 257 285 inline float rnd() 258 286 { 259 return r and() / (RAND_MAX + 1.0f);287 return rnd(0, 1); 260 288 } 261 289 … … 266 294 inline float rnd(float max) 267 295 { 268 return rnd() * max; 269 } 270 271 /** 272 @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>. 273 @param min The minimum 274 @param max The maximum 275 */ 276 inline float rnd(float min, float max) 277 { 278 return rnd(max - min) + min; 296 return rnd(0, max); 279 297 } 280 298 … … 284 302 inline float rndsgn() 285 303 { 286 return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0 304 std::uniform_int_distribution<> dist; 305 return static_cast<float>((dist(detail::rngen) & 0x2) - 1); // dist(...) & 0x2 is either 2 or 0 287 306 } 288 307
Note: See TracChangeset
for help on using the changeset viewer.