From 9d51572cbe6d54b1595285c7fb6d2fae7d801fd3 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Tue, 11 Dec 2007 10:02:48 +0000 Subject: rework the random numbers API --- src/Core/NumTraits.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Core/NumTraits.h b/src/Core/NumTraits.h index 75617aed4..2c0040397 100644 --- a/src/Core/NumTraits.h +++ b/src/Core/NumTraits.h @@ -57,6 +57,7 @@ template struct NumTraits > }; template inline typename NumTraits::Real precision(); +template inline T random(const T& a, const T& b); template inline T random(); template<> inline int precision() { return 0; } @@ -74,12 +75,17 @@ inline int sqrt(const int& x) assert(false); return 0; } +template<> inline int random(const int& a, const int& b) +{ + // We can't just do rand()%n as only the high-order bits are really random + return a + static_cast((b-a+1) * (rand() / (RAND_MAX + 1.0))); +} template<> inline int random() { // "rand() % n" is bad, they say, because the low-order bits are not random enough. // However here, 21 is odd, so random() % 21 uses the high-order bits // as well, so there's no problem. - return (std::rand() % 21) - 10; + return random(-10, 10); } inline bool isMuchSmallerThan(const int& a, const int& b, const int& prec = precision()) { @@ -105,9 +111,13 @@ inline float conj(const float& x) { return x; } inline float abs(const float& x) { return std::abs(x); } inline float abs2(const float& x) { return x*x; } inline float sqrt(const float& x) { return std::sqrt(x); } +template<> inline float random(const float& a, const float& b) +{ + return a + (b-a) * std::rand() / RAND_MAX; +} template<> inline float random() { - return std::rand() / (RAND_MAX/20.0f) - 10.0f; + return random(-10.0f, 10.0f); } inline bool isMuchSmallerThan(const float& a, const float& b, const float& prec = precision()) { @@ -129,9 +139,13 @@ inline double conj(const double& x) { return x; } inline double abs(const double& x) { return std::abs(x); } inline double abs2(const double& x) { return x*x; } inline double sqrt(const double& x) { return std::sqrt(x); } +template<> inline double random(const double& a, const double& b) +{ + return a + (b-a) * std::rand() / RAND_MAX; +} template<> inline double random() { - return std::rand() / (RAND_MAX/20.0) - 10.0; + return random(-10.0, 10.0); } inline bool isMuchSmallerThan(const double& a, const double& b, const double& prec = precision()) { -- cgit v1.2.3