Changes for version 1.20

  • Added a Uniform option to makerandom() and makerandom_itv() that doesn't set the high bit of the generated random, and produces a number uniformally distributed in the interval. Thanks to Len Budney for pointing this out.
    • Here's his explaination:
    • If Size is increased to 4 bits, then of 16 values, there are 5 ways to get a 1, 5 ways to get a 2, but 6 ways to get a 0. Thus 0 will occur 6/5 = 1.2 times more often than 1 or 2. Increasing size to 6 bits, there are (2^6-1)/3 ways to get a 1 or 2, but there are (2^6+2)/3 ways to get a 0, and a 0 will occur more often by a factor of (2^6+2)/(2^6-1).
    • If Size is then bumped up to 40, as makerandom_itv does, then 0 will still happen more often than 1 or 2, but now it happens only (2^40+2)/(2^40-1) times more often, or about 1 + 2.7*10^-12 times more often. This difference is too small to observe in a practical experiment, but it is still a deviation from uniformity.
    • What's going on is that when ($b-$a) does not evenly divide 2^N, the outcome ($b-1) is less likely to happen than the outcome $a. The case that Size=2, $a=0 and $b=3 illustrates this most clearly.
    • This is easy to correct: we simply want to make the largest value of makerandom() to be evenly divisible by ($b-$a), by ignoring any result which is >= (2^Size - (2^Size % ($b-$a))). The second attached file includes the fix, which you will see produces uniform probabilities if you perform the test given above.

Modules

Cryptographically Secure, True Random Number Generator.

Provides

in lib/Crypt/Random/Generator.pm
in lib/Crypt/Random/Provider/File.pm
in lib/Crypt/Random/Provider/devrandom.pm
in lib/Crypt/Random/Provider/devurandom.pm
in lib/Crypt/Random/Provider/egd.pm
in lib/Crypt/Random/Provider/rand.pm