From 738fbc66d096575bb9a1694056ce2d627a70c03d Mon Sep 17 00:00:00 2001 From: est31 Date: Tue, 11 Aug 2015 19:07:56 +0200 Subject: Fix Lua PcgRandom Before, this lua code led to a crash: local pcg = PcgRandom(42) local value = pcg:next() This was because if you called s32 PcgRandom::range(min, max) with the minimum and maximum possible values for s32 integers (which the lua binding code did), u32 PcgRandom::range(bound) got called with 0 as the bound. The bound however is one above the maximum value, so 0 is a "special" value to pass to this function. This commit fixes the lua crash by assigning the RNG's full range to the bound 0, which is also fits to the "maximum is bound - 1" principle, as (u32)-1 is the maximum value in the u32 range. --- src/noise.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/noise.cpp') diff --git a/src/noise.cpp b/src/noise.cpp index 443c405ce..2948fb765 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -90,6 +90,9 @@ u32 PcgRandom::next() 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. -- cgit v1.2.3