From 34e894ed6fbb5af0dcd0a61b02de75d6044aede8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Wed, 19 Apr 2017 22:11:32 +0200 Subject: Fix dgreftest. --- src/random.cc | 26 ++++++++------------------ src/random.h | 4 ---- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/random.cc b/src/random.cc index f6b5b34..9317744 100644 --- a/src/random.cc +++ b/src/random.cc @@ -103,28 +103,18 @@ float Random::floatInRange(float lower_bound, float upper_bound) // https://en.wikipedia.org/wiki/Marsaglia_polar_method float Random::normalDistribution(float mean, float stddev) { - if (has_saved_value) + float u, v, s; + do { - has_saved_value = false; - return saved_value * stddev + mean; + u = 2.0*generateFloat() - 1.0; + v = 2.0*generateFloat() - 1.0; + s = (u * u) + (v * v); } - else - { - float u, v, s; - do - { - u = 2.0f*generateFloat() - 1; - v = 2.0f*generateFloat() - 1; - s = (u * u) + (v * v); - } - while (s > 1.0f || s == 0.0f); + while (s > 1.0 || s == 0.0); - s = std::sqrt(-2*std::log(s) / s); - saved_value = u * s; - has_saved_value = true; + s = std::sqrt(-2*std::log(s) / s); - return mean + stddev * (v * s); - } + return stddev * (v * s) + mean; } float Random::generateFloat() diff --git a/src/random.h b/src/random.h index 7f53c05..cdf7705 100644 --- a/src/random.h +++ b/src/random.h @@ -58,10 +58,6 @@ private: std::minstd_rand0 generator; float generateFloat(); - - // variables for normalDistribution - bool has_saved_value = false; - float saved_value; }; template -- cgit v1.2.3