From c21c7e77e93b116f1f06fe6af1e1ef1e18c204fb Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 1 Oct 2021 16:00:22 +0100 Subject: [PATCH] crand::next_u64() now correctly returns uniformly between 0..=RANGE_MAX MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for cpprng's current commit: Small blessing − 小吉 --- src/rng/crand.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rng/crand.cpp b/src/rng/crand.cpp index ef0fb39..50314ce 100644 --- a/src/rng/crand.cpp +++ b/src/rng/crand.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -30,7 +31,7 @@ namespace rng //TODO: properly implemet this f64 crand::_sample() { return (f64)std::bit_cast(_sample_raw()) / (f64)UINT64_MAX; } // Overrides // - u64 crand::next_u64() { return std::bit_cast(_sample_raw() & INT32_MAX); } // remove sign bit because it messes with the range. + u64 crand::next_u64() { return std::bit_cast(std::abs(_sample_raw()) & INT32_MAX); } // remove sign bit because it messes with the range. //TODO: Implementing next_i64(i64 max) should be trivial. the man page for drand48 shows its output range, just map that range to `0..=max` //TODO: make the default range for all non-bounded `next_i/u*` the same range as drand48's. If that's too big for the integer type, scale it down.