diff options
author | Søren Sandmann Pedersen <ssp@redhat.com> | 2011-08-03 18:38:20 -0400 |
---|---|---|
committer | Søren Sandmann Pedersen <ssp@redhat.com> | 2011-08-11 03:32:14 -0400 |
commit | 842591d9d12a24a9a06308ae03996153c5a99e64 (patch) | |
tree | e888530c7abd588fedc4df34bbc82b05ca07b920 | |
parent | 12da53f81c4a507a963641796132bbafe0cd6224 (diff) |
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.
-rw-r--r-- | test/utils.h | 12 |
1 files 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 |