summaryrefslogtreecommitdiff
path: root/src/noise.cpp
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2016-06-04 02:16:06 -0400
committerkwolekr <kwolekr@minetest.net>2016-06-04 02:16:06 -0400
commit8ed467d438634ffe45806a6a6a325bb00774d651 (patch)
treee699568c7b3b91ee37e85ed3cf8880d63b3917cb /src/noise.cpp
parentdfbdb5bcd7bc48efb21d585d5c22454a9d5f0f1e (diff)
downloadminetest-8ed467d438634ffe45806a6a6a325bb00774d651.tar.gz
minetest-8ed467d438634ffe45806a6a6a325bb00774d651.tar.bz2
minetest-8ed467d438634ffe45806a6a6a325bb00774d651.zip
PcgRandom: Fix/improve documentation
Diffstat (limited to 'src/noise.cpp')
-rw-r--r--src/noise.cpp31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/noise.cpp b/src/noise.cpp
index c57c98ccb..b918c9936 100644
--- a/src/noise.cpp
+++ b/src/noise.cpp
@@ -93,22 +93,31 @@ u32 PcgRandom::range(u32 bound)
// If the bound is 0, we cover the whole RNG's range
if (bound == 0)
return next();
+
/*
- If the bound is not a multiple of the RNG's range, it may cause bias,
- e.g. a RNG has a range from 0 to 3 and we take want a number 0 to 2.
- Using rand() % 3, the number 0 would be twice as likely to appear.
- With a very large RNG range, the effect becomes less prevalent but
- still present. This can be solved by modifying the range of the RNG
- to become a multiple of bound by dropping values above the a threshold.
- In our example, threshold == 4 - 3 = 1 % 3 == 1, so reject 0, thus
- making the range 3 with no bias.
-
- This loop looks dangerous, but will always terminate due to the
- RNG's property of uniformity.
+ This is an optimization of the expression:
+ 0x100000000ull % bound
+ since 64-bit modulo operations typically much slower than 32.
*/
u32 threshold = -bound % bound;
u32 r;
+ /*
+ If the bound is not a multiple of the RNG's range, it may cause bias,
+ e.g. a RNG has a range from 0 to 3 and we take want a number 0 to 2.
+ Using rand() % 3, the number 0 would be twice as likely to appear.
+ With a very large RNG range, the effect becomes less prevalent but
+ still present.
+
+ This can be solved by modifying the range of the RNG to become a
+ multiple of bound by dropping values above the a threshold.
+
+ In our example, threshold == 4 % 3 == 1, so reject values < 1
+ (that is, 0), thus making the range == 3 with no bias.
+
+ This loop may look dangerous, but will always terminate due to the
+ RNG's property of uniformity.
+ */
while ((r = next()) < threshold)
;