Eric De Mund wrote:
Ron, John Hasler: ] echo $(( $(( $RANDOM - $RANDOM )) % 3 )) Ron Johnson: ] Does this burn through entropy faster than other methods? Yes. Twice as fast. And, more importantly, it's mathematically incorrect. It does not have the same probability density function as: echo $(( $(($RANDOM % 5)) - 2))
To be complete, (( $(($RANDOM % 5)) - 2)) is not perfect either, but closer to perfection if $RANDOM is a true random generator.
To simplify things, let's assume that the numbers we work on are 4 bits long. The possible values are:
$RANDOM -> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $RANDOM % 5 -> 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 You see that 0 has a higher probability than the other values.If Max is the biggest int that can be generated by $RANDOM, and N is the number of different values we need, we should throw away any value of $RANDOM that is >= int(Max / N) * N.
Just my 2 cents ;-) François.