[issue41274] Better way to random.seed()?

Raymond Hettinger report at bugs.python.org
Fri Jul 10 20:12:59 EDT 2020


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

> is it possible to write a new module that overrides the seed()
> method in the random library in its initialization code and 
> replaces it with this method of seeding the generator?

Yes.  The simplest way is override seed() in a subclass.  You can put that subclass in a new module if you like and can even call the class "Random" if desired.

>>> class InputRandom(random.Random):
	def seed(self, a=None):
		if a is None:
			a = int(input('Enter a seed: '))
		super().seed(a)

		
>>> r = InputRandom()
Enter a seed: 1234
>>> r.random()
0.9664535356921388
>>> r.random()
0.4407325991753527
>>> r.seed()
Enter a seed: 1234
>>> r.random()
0.9664535356921388
>>> r.seed(1234)
>>> r.random()
0.9664535356921388

----------
nosy: +rhettinger

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41274>
_______________________________________


More information about the Python-bugs-list mailing list