Skip to content

Determinism and compatibility of random module #1269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HyeonuPark opened this issue Aug 15, 2019 · 2 comments · Fixed by #1731
Closed

Determinism and compatibility of random module #1269

HyeonuPark opened this issue Aug 15, 2019 · 2 comments · Fixed by #1731

Comments

@HyeonuPark
Copy link
Contributor

Should random.random() always be same when same random.seed() is applied? Should this be same across interpreter process? Should this be same across RustPython version? Should this be same with how CPython does?

Python document states this in https://docs.python.org/3/library/random.html#notes-on-reproducibility

Sometimes it is useful to be able to reproduce the sequences given by a pseudo random number generator. By re-using a seed value, the same sequence should be reproducible from run to run as long as multiple threads are not running.

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.

random.seed() takes version argument which can be 1 or 2.

With version 2 (the default), a str, bytes, or bytearray object gets converted to an int and all of its bits are used.

With version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.

@sanxiyn
Copy link
Contributor

sanxiyn commented Nov 1, 2019

In CPython, random.seed version handling and stuffs are implemented in Python in random.py, which uses _random C module. Current RustPython implementation is reimplementing random, not _random, so can't share Python part.

#1571 is an effort to fix it which should help this issue.

@vks
Copy link

vks commented Nov 19, 2019

I think it is best to use a modern RNG by default (i.e. rand::rngs::StdRng), possibly falling back to the Mersenne Twister when reproducibility is required (i.e. when seed is called).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants