summaryrefslogtreecommitdiff
path: root/src/random.cc
diff options
context:
space:
mode:
authorAndré Nusser <andre.nusser@googlemail.com>2017-04-19 22:11:32 +0200
committerAndré Nusser <andre.nusser@googlemail.com>2017-04-19 22:11:32 +0200
commit34e894ed6fbb5af0dcd0a61b02de75d6044aede8 (patch)
treec4a5b09239cda6928ef8e332d1c601df7ba4b9e2 /src/random.cc
parentf5f63d464ee6abc87587678d0cdfd16ebd4e3d91 (diff)
Fix dgreftest.
Diffstat (limited to 'src/random.cc')
-rw-r--r--src/random.cc26
1 files changed, 8 insertions, 18 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()