diff options
author | toasteater <48371905+toasteater@users.noreply.github.com> | 2019-03-18 11:41:16 +0800 |
---|---|---|
committer | toasteater <48371905+toasteater@users.noreply.github.com> | 2019-03-19 18:14:58 +0800 |
commit | 5f1b9a23132408160cf0b99e1e162413ab0e382b (patch) | |
tree | 631b214908b1af14f18872555d0cbe83886c9c43 /core/math/random_pcg.cpp | |
parent | df7d3708c5b535c3696943322a14ec19a175e30c (diff) | |
download | redot-engine-5f1b9a23132408160cf0b99e1e162413ab0e382b.tar.gz |
Improved uniformity of RandomPCG::randf.
When generating single precision floats, Godot casts a uint32_t to float,
causing uniformity loss.
This new randf, inspired by T. R. Campbell's random_real, samples the output
of rand as the fraction part of an infinite binary number, with some tricks
to reduce ops and branching. This method provides "good enough" uniformity at
decent speed, for floats greater than 2^-64. Smaller numbers are floored to 0.
Diffstat (limited to 'core/math/random_pcg.cpp')
-rw-r--r-- | core/math/random_pcg.cpp | 8 |
1 files changed, 2 insertions, 6 deletions
diff --git a/core/math/random_pcg.cpp b/core/math/random_pcg.cpp index 45467b32b2..b82b24ac4d 100644 --- a/core/math/random_pcg.cpp +++ b/core/math/random_pcg.cpp @@ -44,13 +44,9 @@ void RandomPCG::randomize() { } double RandomPCG::random(double p_from, double p_to) { - unsigned int r = rand(); - double ret = (double)r / (double)RANDOM_MAX; - return (ret) * (p_to - p_from) + p_from; + return randd() * (p_to - p_from) + p_from; } float RandomPCG::random(float p_from, float p_to) { - unsigned int r = rand(); - float ret = (float)r / (float)RANDOM_MAX; - return (ret) * (p_to - p_from) + p_from; + return randf() * (p_to - p_from) + p_from; } |