difference between random module in python 2.6 and 3.2?

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Tue Feb 7 16:11:04 CET 2012


Am 06.02.2012 09:45, schrieb Matej Cepl:
> Also, how could I write a re-implementation of random.choice which would
> work same on python 2.6 and python 3.2? It is not only matter of unit
> tests, but I would really welcome if the results on both versions
> produce the same results.

Two approaches come to mind:
1. make two runs and verify that they differ
This is a bit problematic, because there is a small but nonzero chance 
that two runs don't differ. Random numbers are just not random enough. 
Anyhow, afterwards, sort the results again and verify that it was just 
the order that differs.

2. hack the random module into something nonrandom
I think you can "import debug_random as random" and then have your 
testee automatically pick up that module instead of the real one. Since 
you already hardcoded the results to check against ("expected = ..." in 
your code), you can also hard-code the sequence of random numbers 
corresponding to that. This is even cleaner, as you don't rely on 
something being deterministic that is supposed to be random, which is 
not totally surprising but still somehow paradox.

Uli



More information about the Python-list mailing list