From 31cf2705653f473b7185537b1f0dcd86f0597051 Mon Sep 17 00:00:00 2001 From: Johanna Amann Date: Wed, 29 Jun 2022 14:37:39 +0200 Subject: [PATCH] Optimize 64 bit random number generation rand64bit called random 4 times to generate one 64 bit number. There is no reason to do this - random() is basically guaranteed to return a 32 bit number. This also adds a static check to make sure that it does. --- src/util.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/util.cc b/src/util.cc index 02c4b0dbbe..3f349d385b 100644 --- a/src/util.cc +++ b/src/util.cc @@ -544,8 +544,9 @@ uint64_t rand64bit() uint64_t base = 0; int i; - for ( i = 1; i <= 4; ++i ) - base = (base << 16) | detail::random_number(); + static_assert(RAND_MAX == 2147483647); // 2^32-1 + for ( i = 1; i <= 2; ++i ) + base = (base << 32) | detail::random_number(); return base; }