Re: difference between random module in python 2.6 and 3.2?



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
.



Relevant Pages

  • Re: problem using differ
    ... > trouble using the differ object. ... For some reason, Python doesn't recognize ... Beautiful is better than ugly.') ...
    (comp.lang.python)
  • Re: direct initialization of class attributes vs. declarations w/in __init__
    ... must admit, your last statement is a bit snide and certainly not ... behavior could be different, even if the python versions would differ ... Frederik (as one of the most profiled python developers out there) might ... Although he dropped what I perceived to be a snide and unhelpful ...
    (comp.lang.python)
  • Re: Coordinate Grid Points
    ... I am very knew to python and am attempting to write a program ... in python that a friend of mine is having to write in java. ... see the random module ...
    (comp.lang.python)
  • Re: May i customize basic operator (such as 1==3)?
    ... It's not good advice in Python programming, ... specialzed classes, particularly if you want the compiler to help ... it better if they are more specific) it still can't verify ...
    (comp.lang.python)
  • Re: compare Fortran modules
    ... are not found in file B, and vice versa ... differ from those with the same name in file B. ... Python or some other language. ... The unix diff command is much more powerful than fc, ...
    (comp.lang.fortran)