3 ms·
There are cases for keeping just the high bits. For example, many language runtime libraries have pseudo random number generators (PRNGs) use a 32-bit linear c
by barrkel 9y ago
There are cases for keeping just the high bits.
For example, many language runtime libraries have pseudo random number generators (PRNGs) use a 32-bit linear congruential generator (LCG). Suppose you want to create a rand(n) function which returns an integer between 0 and n-1. One good way of doing this is multiplying the lcg_output (a 32-bit unsigned integer) by n, and returning the high 32 bits.
It's better to do this than a modulus operation because the low bits on LCG sequences are highly predictable.
(This is how the Delphi Random(n) function works.)