From 6a61981e0480b8a469d9d4b84ea27a9ec1d2b8d9 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 6 Jan 2023 15:51:30 +0000 Subject: awk: make random values more random When srand(3) is called to seed the random number generator with the current time the first value returned by rand(3) changes slowly with time. This is a property of the implementation in the C runtime. Change the order in which values from rand(3) are consumed to generate the value returned by the awk rand() function. This puts the value returned by the first call to rand(3) in the least significant bits, not the most significant. (GitHub issue #279) --- editors/awk.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/editors/awk.c b/editors/awk.c index eedb7aa84..342b93df9 100644 --- a/editors/awk.c +++ b/editors/awk.c @@ -3284,9 +3284,9 @@ static var *evaluate(node *op, var *res) R_d = (double)v / 0x8000000000000000ULL; #elif ENABLE_PLATFORM_MINGW32 && RAND_MAX == 0x7fff /* 45 bits of randomness ought to be enough for anyone */ - uint64_t v = ((uint64_t)rand() << 48) | + uint64_t v = ((uint64_t)rand() << 18) | ((uint64_t)rand() << 33) | - ((uint64_t)rand() << 18); + ((uint64_t)rand() << 48); R_d = (double)v / 0x8000000000000000ULL; #else # error Not implemented for this value of RAND_MAX -- cgit v1.2.3-55-g6feb