From 842591d9d12a24a9a06308ae03996153c5a99e64 Mon Sep 17 00:00:00 2001 From: Søren Sandmann Pedersen Date: Wed, 3 Aug 2011 18:38:20 -0400 Subject: Fix lcg_rand_u32() to return 32 random bits. The lcg_rand() function only returns 15 random bits, so lcg_rand_u32() would always have 0 in bit 31 and bit 15. Fix that by calling lcg_rand() three times, to generate 15, 15, and 2 random bits respectively. V2: Use the 10/11 most significant bits from the 3 lcg results and mix them with the low ones from the adjacent one, as suggested by Andrea Canciani. --- test/utils.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/utils.h b/test/utils.h index 615ad78..000aaa6 100644 --- a/test/utils.h +++ b/test/utils.h @@ -44,10 +44,14 @@ lcg_rand_N (int max) static inline uint32_t lcg_rand_u32 (void) { - uint32_t lo = lcg_rand(); - uint32_t hi = lcg_rand(); - - return (hi << 16) | lo; + /* This uses the 10/11 most significant bits from the 3 lcg results + * (and mixes them with the low from the adjacent one). + */ + uint32_t lo = lcg_rand() >> -(32 - 15 - 11 * 2); + uint32_t mid = lcg_rand() << (32 - 15 - 11 * 1); + uint32_t hi = lcg_rand() << (32 - 15 - 11 * 0); + + return (hi ^ mid ^ lo); } /* CRC 32 computation -- cgit v1.2.3