generate random numbers in a deterministic way

Temia Eszteri lamialily at cleverpun.com
Wed Apr 25 02:01:06 EDT 2012


Assuming you're using the Python's random module, which works based on
the Mersenne Twister, you can preset the seed with
random.seed(hashable).

More details here:
http://docs.python.org/library/random.html#random.seed

~Temia

On Wed, 25 Apr 2012 07:51:18 +0200, you wrote:

>Hi,
>
>I'm working with some sorting algorithms and I want to compare their
>efficiency. One test fills a list with one million random integers,
>which serves as input for the algorithms. However, if this list is
>different each time I run the tests, the tests wouldn't be fair. At
>the moment the selected sorting alg. can be specified with a switch
>and only one alg. is tested each time.
>
>So what I want is the following: fill the list with random numbers,
>but when I re-execute the script, I want the same "random" numbers in
>the same order. This way each sorting alg. would get the same input.
>
>As a workaround, I made a generator that outputs a random list in a
>file, and this list is read each time by the testing script. I just
>wonder if there is a more elegant solution.
>
>Thanks,
>
>Laszlo
--
When on earth, do as the earthlings do.



More information about the Python-list mailing list