[Orxonox-commit 6155] r10813 - in code/branches/cpp11_v2/src/libraries: core util
muemart at orxonox.net
muemart at orxonox.net
Tue Nov 17 18:28:25 CET 2015
Author: muemart
Date: 2015-11-17 18:28:25 +0100 (Tue, 17 Nov 2015)
New Revision: 10813
Modified:
code/branches/cpp11_v2/src/libraries/core/CoreConfig.cc
code/branches/cpp11_v2/src/libraries/util/Math.cc
code/branches/cpp11_v2/src/libraries/util/Math.h
Log:
Use new random number generation functions
Modified: code/branches/cpp11_v2/src/libraries/core/CoreConfig.cc
===================================================================
--- code/branches/cpp11_v2/src/libraries/core/CoreConfig.cc 2015-11-16 15:30:49 UTC (rev 10812)
+++ code/branches/cpp11_v2/src/libraries/core/CoreConfig.cc 2015-11-17 17:28:25 UTC (rev 10813)
@@ -35,6 +35,8 @@
#include "core/Language.h"
#include "core/ApplicationPaths.h"
+#include <random>
+
namespace orxonox
{
RegisterClassNoArgs(CoreConfig);
@@ -128,7 +130,10 @@
static bool bInitialized = false;
if (!bInitialized && this->bInitRandomNumberGenerator_)
{
- srand(static_cast<unsigned int>(time(0)));
+ std::random_device rnddev;
+ rndseed(rnddev());
+ //Keep the old seeding around because people will probably still use the old functions
+ srand(rnddev());
rand();
bInitialized = true;
}
Modified: code/branches/cpp11_v2/src/libraries/util/Math.cc
===================================================================
--- code/branches/cpp11_v2/src/libraries/util/Math.cc 2015-11-16 15:30:49 UTC (rev 10812)
+++ code/branches/cpp11_v2/src/libraries/util/Math.cc 2015-11-17 17:28:25 UTC (rev 10813)
@@ -370,6 +370,12 @@
return (targetposition + targetvelocity * time);
}
+
+ namespace detail
+ {
+ std::mt19937 rngen;
+ }
+
/**
@brief Returns a unique number. This function will never return the same value twice.
*/
Modified: code/branches/cpp11_v2/src/libraries/util/Math.h
===================================================================
--- code/branches/cpp11_v2/src/libraries/util/Math.h 2015-11-16 15:30:49 UTC (rev 10812)
+++ code/branches/cpp11_v2/src/libraries/util/Math.h 2015-11-17 17:28:25 UTC (rev 10813)
@@ -45,6 +45,7 @@
#include <string>
#include <cmath>
#include <cstdlib>
+#include <random>
#include <OgreMath.h>
#include <OgreVector2.h>
@@ -251,31 +252,48 @@
return static_cast<T>((-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start);
}
+ namespace detail
+ {
+ /**
+ Random number generator used for the functions below. Marked extern to only have one global instance.
+ */
+ _UtilExport extern std::mt19937 rngen;
+ }
+
/**
- @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>.
+ @brief Seeds the random number generator used for the functions below.
*/
- inline float rnd()
+ inline void rndseed(unsigned int seed)
{
- return rand() / (RAND_MAX + 1.0f);
+ detail::rngen.seed(seed);
}
/**
- @brief Returns a random number between 0 and almost @a max: <tt>0 <= rnd < max</tt>.
- @param max The maximum
+ @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>.
+ @param min The minimum
+ @param max The maximum
*/
- inline float rnd(float max)
+ inline float rnd(float min, float max)
{
- return rnd() * max;
+ std::uniform_real_distribution<float> dist(min, max);
+ return dist(detail::rngen);
}
/**
- @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>.
- @param min The minimum
+ @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>.
+ */
+ inline float rnd()
+ {
+ return rnd(0, 1);
+ }
+
+ /**
+ @brief Returns a random number between 0 and almost @a max: <tt>0 <= rnd < max</tt>.
@param max The maximum
*/
- inline float rnd(float min, float max)
+ inline float rnd(float max)
{
- return rnd(max - min) + min;
+ return rnd(0, max);
}
/**
@@ -283,7 +301,8 @@
*/
inline float rndsgn()
{
- return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0
+ std::uniform_int_distribution<> dist;
+ return static_cast<float>((dist(detail::rngen) & 0x2) - 1); // dist(...) & 0x2 is either 2 or 0
}
_UtilExport unsigned long getUniqueNumber();
More information about the Orxonox-commit
mailing list