random changes

Tim Peters tim.one at comcast.net
Fri Aug 8 19:03:33 CEST 2003


[Chris Reedy]
> Here are some interesting numbers:
>
>  >>> def printstats(rng, N=20000):
>          # Cribbed from random._test_generator
> 	import math
> 	sum = 0
> 	sumsq = 0
> 	for i in range(N):
> 		x = rng()
> 		sum += x
> 		sumsq += x*x
> 	avg = sum/N
> 	stddev = math.sqrt(sumsq/N - avg*avg)
> 	print repr(avg), repr(stddev)
>
> Under 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)]:  >>>
>  rng = random.Random()
>  >>> rng.seed(123456789)
>  >>> printstats(rng.random)
> 0.50177345778280169 0.28856727700571438
>  >>> rng.seed(123456789)
>  >>> printstats(lambda : rng.normalvariate(0.0, 1.0))
> 0.0021149783637115457 0.99131361156374964
>
> Under 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)]:
>  >>> rng = random.WichmannHill()
>  >>> rng.seed(123456789)
>  >>> printstats(rng.random)
> 0.50177345778280169 0.28856727700571438
>  >>> rng.seed(123456789)
>  >>> printstats(lambda : rng.normalvariate(0.0, 1.0))
> 0.0064296479176113733 1.0050714625654731

Odd!  I don't have Python 2.2.2 anymore.  Under Python 2.2.3, the results
are identical (and match the results you got for 2.3).

C:\Code>type temp.py
import random
import sys

def printstats(rng, N=20000):
    # Cribbed from random._test_generator
    import math
    sum = 0
    sumsq = 0
    for i in range(N):
        x = rng()
        sum += x
        sumsq += x*x
    avg = sum/N
    stddev = math.sqrt(sumsq/N - avg*avg)
    print repr(avg), repr(stddev)

print sys.version_info
if sys.version_info >= (2, 3):
    rng = random.WichmannHill()
else:
    rng = random.Random()
rng.seed(123456789)
printstats(rng.random)
rng.seed(123456789)
printstats(lambda: rng.normalvariate(0.0, 1.0))

C:\Code>\python22\python temp.py
(2, 2, 3, 'final', 0)
0.50177345778280169 0.28856727700571438
0.0064296479176113733 1.0050714625654731

C:\Code>\python23\python temp.py
(2, 3, 0, 'final', 0)
0.50177345778280169 0.28856727700571438
0.0064296479176113733 1.0050714625654731

I have to go back to 2.1.3 to get your 2.2.2 results:

C:\Code>\python21\python temp.py
(2, 1, 3, 'final', 0)
0.50177345778280169 0.28856727700571438
0.0021149783637115457 0.99131361156374964

As Raymond pointed out in another post, the WH seed method was changed in a
bugfix release, because it had bad flaws.  The old method is still available
in post-bugfix releases under the name .whseed(); use of it can't be
recommended, though.






More information about the Python-list mailing list