Using SHA1 as RNG
Klaus Alexander Seistrup
spam at magnetic-ink.dk
Fri Mar 14 14:55:13 EST 2003
Irmen de Jong wrote:
>>> Even if the hash doesn't repeat itself, you still get a 100%
>>> predictible sequence of numbers because the SHA algorithm is
>>> repeatable.
>>
>> But the same goes for the original Wichmann-Hill generator,
>> doesn't it?
>
> Umm.. perhaps. I don't know that much about strong random number
> generators. Can anybody enlighten me (us)?
The Wichmann-Hill PRNG is basically multiplication and modulus with
2×3 known constants (snipped from whrandom.py):
#v+
def random(self):
"""Get the next random number in the range [0.0, 1.0)."""
# This part is thread-unsafe:
# BEGIN CRITICAL SECTION
x, y, z = self._seed
#
x = (171 * x) % 30269
y = (172 * y) % 30307
z = (170 * z) % 30323
#
self._seed = x, y, z
# END CRITICAL SECTION
#
return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
#v-
Start with the same seeds, and you end up with the same sequence
of numbers. Oh, and it seems the cycle for the Wichmann-Hill RNG
is not 30268*30306*30322-1, but (30268*30306*30322-1)/4. See (¹).
I believe the strength of the W-H generator is that it is portable
and computational inexpensive.
// Klaus
(¹) <http://www.math.montana.edu/Rweb/Rhelp/Random.html>
--
><> unselfish actions pay back better
More information about the Python-list
mailing list