aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-01-06 15:51:30 +0000
committerRon Yorston <rmy@pobox.com>2023-01-06 15:51:30 +0000
commit6a61981e0480b8a469d9d4b84ea27a9ec1d2b8d9 (patch)
tree76e2f4c2070676618e2ff8d5d143301d47ad21e8
parent1eb579690c66bd3ca893aefaa77d17be7dda78b5 (diff)
downloadbusybox-w32-6a61981e0480b8a469d9d4b84ea27a9ec1d2b8d9.tar.gz
busybox-w32-6a61981e0480b8a469d9d4b84ea27a9ec1d2b8d9.tar.bz2
busybox-w32-6a61981e0480b8a469d9d4b84ea27a9ec1d2b8d9.zip
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)
-rw-r--r--editors/awk.c4
1 files 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)
3284 R_d = (double)v / 0x8000000000000000ULL; 3284 R_d = (double)v / 0x8000000000000000ULL;
3285#elif ENABLE_PLATFORM_MINGW32 && RAND_MAX == 0x7fff 3285#elif ENABLE_PLATFORM_MINGW32 && RAND_MAX == 0x7fff
3286 /* 45 bits of randomness ought to be enough for anyone */ 3286 /* 45 bits of randomness ought to be enough for anyone */
3287 uint64_t v = ((uint64_t)rand() << 48) | 3287 uint64_t v = ((uint64_t)rand() << 18) |
3288 ((uint64_t)rand() << 33) | 3288 ((uint64_t)rand() << 33) |
3289 ((uint64_t)rand() << 18); 3289 ((uint64_t)rand() << 48);
3290 R_d = (double)v / 0x8000000000000000ULL; 3290 R_d = (double)v / 0x8000000000000000ULL;
3291#else 3291#else
3292# error Not implemented for this value of RAND_MAX 3292# error Not implemented for this value of RAND_MAX