Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is seeding the random generator not stable between versions of Python?

Tags:

People also ask

How does random seed work in Python?

Python Random seed() Method The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.

What is the purpose of seeding a random number generator?

The purpose of the seed is to allow the user to "lock" the pseudo-random number generator, to allow replicable analysis. Some analysts like to set the seed using a true random-number generator (TRNG) which uses hardware inputs to generate an initial seed number, and then report this as a locked number.

Does random seed affect NumPy?

seed(number) sets what NumPy calls the global random seed — affecting all uses to the np.

Do you need a seed Python?

The module actually seeds the generator (with OS-provided random data from urandom if possible, otherwise with the current date and time) when you import the module, so there's no need to manually call seed() .


I am trying to reproduce a random sequence from python's random.random() on a different system with a different python3 version installed.

This should be easy as the documentation says:

Most of the random module’s algorithms and seeding functions are subject to change across Python versions, but two aspects are guaranteed not to change:

  • If a new seeding method is added, then a backward compatible seeder will be offered.
  • The generator’s random() method will continue to produce the same sequence when the compatible seeder is given the same seed.

So I expect the following code to print always the same 10 numbers, no matter the specific python3 version:

import sys print(sys.version)  from random import seed, random  seed(str(1)) for i in range(10):     print(random()) 

However, testing it on two different machines:

3.2.3 (default, May  3 2012, 15:51:42)  [GCC 4.6.3] 0.4782479962566343 0.044242767098090496 0.11703586901195051 0.8566892547933538 0.2926790185279551 0.0067328440779825804 0.0013279506360178717 0.22167546902173108 0.9864945747444945 0.5157002525757287 

and

3.1.2 (release31-maint, Dec  9 2011, 20:59:40)   [GCC 4.4.5] 0.0698436845523 0.27772471476 0.833036057868 0.35569897036 0.36366158783 0.722487971761 0.963133581734 0.263723867191 0.451002768569 0.0998765577881 

Give different results.

Why is this? And is there any way to make this to work (i.e. get the same random sequence twice?)