Re: Entropy (was Re: how to generate random negative numbers)
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 illustrate, assuming that each $RANDOM value is uniform over {0,1,2}
mod 3, here is the table of the function b-a:
b
+-------------
| | 0 1 2
+-+-----------
|0| 0 1 2
a|1| -1 0 1
|2| -2 -1 0
5 (the number of values in {-2,-1,0,1,2}) simply doesn't divide 9 (the
number of values in {0,1,2}x{0,1,2})
(Also, remember that when one adds multiple random deviates, as the
number of deviates approaches infinity, the distribution approaches
Gaussian. Here, with two deviates, one ends up with a "triangle"
distribution.)
Programmatic cleverness sometimes works, but it's a sharp knife. One
must be careful not to accidentally cut oneself.
Regards,
Eric
--
Eric De Mund | Y!: ead0002
ead@ixian.com | ICQ: 811788
Reply to: