Changeset 10813 for code/branches/cpp11_v2/src/libraries/util/Math.h
- Timestamp:
- Nov 17, 2015, 6:28:25 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.