From 31daabcf0a6228bf10b94debe06547312c009929 Mon Sep 17 00:00:00 2001 From: Pavlo Ivashkov Date: Mon, 6 Jan 2025 23:08:00 +0200 Subject: [PATCH] AK: Improve performance of u64 randoms --- AK/Random.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/AK/Random.cpp b/AK/Random.cpp index 5f89c77d496..02082cde29e 100644 --- a/AK/Random.cpp +++ b/AK/Random.cpp @@ -5,21 +5,19 @@ */ #include -#include -#include namespace AK { u32 get_random_uniform(u32 max_bounds) { // If we try to divide all 2**32 numbers into groups of "max_bounds" numbers, we may end up - // with a group around 2**32-1 that is a bit too small. For this reason, the implementation - // `arc4random() % max_bounds` would be insufficient. Here we compute the last number of the - // last "full group". Note that if max_bounds is a divisor of UINT32_MAX, - // then we end up with UINT32_MAX: - u32 const max_usable = UINT32_MAX - (static_cast(UINT32_MAX) + 1) % max_bounds; + // with one group that is a bit too small. For this reason, the implementation + // `arc4random() % max_bounds` would be insufficient. Here we compute the first number of + // the first "full group" as 2**32 % max_bounds, and rely on the equivalence + // `2**32 % x == (2**32 - x) % x` to keep calculations in u32 domain: + u32 const threshold = static_cast(-max_bounds) % max_bounds; auto random_value = get_random(); - for (int i = 0; i < 20 && random_value > max_usable; ++i) { + for (int i = 0; i < 20 && random_value < threshold; ++i) { // By chance we picked a value from the incomplete group. Note that this group has size at // most 2**31-1, so picking this group has a chance of less than 50%. // In practice, this means that for the worst possible input, there is still only a @@ -33,10 +31,10 @@ u32 get_random_uniform(u32 max_bounds) u64 get_random_uniform_64(u64 max_bounds) { // Uses the same algorithm as `get_random_uniform`, - // by replacing u64 with u128 and u32 with u64. - u64 const max_usable = UINT64_MAX - static_cast((static_cast(UINT64_MAX) + 1) % max_bounds); + // by replacing u32 with u64. + u64 const threshold = static_cast(-max_bounds) % max_bounds; auto random_value = get_random(); - for (int i = 0; i < 20 && random_value > max_usable; ++i) { + for (int i = 0; i < 20 && random_value < threshold; ++i) { random_value = get_random(); } return random_value % max_bounds;