From 585de0d318e12859aff6581e42619f1fb1aecc37 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sun, 23 Aug 2020 16:19:49 -0400 Subject: [PATCH] Use more precise conversion from uint64_t -> float on [0,1) for `random::uniform()`. --- src/random.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/random.cpp b/src/random.cpp index 678e1e3d..351b3bc2 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -57,8 +57,8 @@ uint64_t u64() { } float uniform() { - // Make sure to only use 24 bits so it's impossible to round up to 1.f. - return (xoroshiro128plus_next() >> (64 - 24)) / std::pow(2.f, 24); + // The multiplier is 2f7fffff in hex. This gives maximum precision of uint32_t -> float conversion and its image is [0, 1). + return u32() * 2.32830629e-10f; } float normal() {