sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.31k stars 451 forks source link

set_random_seed does not seed Python's random number generator #12580

Open a9bc4e33-7b98-4180-affc-8dfcef89e22b opened 12 years ago

a9bc4e33-7b98-4180-affc-8dfcef89e22b commented 12 years ago

William Stein reported on sage.devel:http://groups.google.com/group/sage-devel/browse_thread/thread/5fa8e919dd83b4b7:

sage: import random
sage: set_random_seed(0); random.randint(0,20)
3
sage: set_random_seed(0); random.randint(0,20)
8

William said "Basically, I expected that Sage's "set_random_seed" would actually set Python's own random seed, given that it sets gp, gap, maxima, etc."

Component: misc

Issue created by migration from https://trac.sagemath.org/ticket/12580

nbruin commented 12 years ago
comment:1

From the documentation of sage.misc.randstate:

Note that wrappers of all the random number generation methods from
Python's :mod:`random` module are available at the Sage command
line, and these wrappers are properly affected by :meth:`set_random_seed`. ::

Indeed,

sage: set_random_seed(0); randint(0,20)
2
sage: set_random_seed(0); randint(0,20)
2
sage: set_random_seed(0); randint(0,20)
2
sage: type(randint(0,20))
<type 'int'>

So, Witty's design was to wrap python's random functionality to get it to respect set_random_seed rather than reach into python and change the seed there. Perhaps he had thread-safety in mind or something like that?

With that design, the behaviour noted in this ticket is not a bug but a feature that arises from circumventing the advertised API.

This ticket can still be considered as a request: can we reconsider the design decisions made in randomstate, of course.

rwst commented 10 years ago
comment:6

Certainly not critical. Not a defect either.