summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/random.cc26
-rw-r--r--src/random.h4
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 <typename T>