classification
Title: Better way to random.seed()?
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: flybd5, remi.lapeyre, rhettinger, steven.daprano
Priority: normal Keywords:

Created on 2020-07-10 21:44 by flybd5, last changed 2020-07-31 02:28 by flybd5. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21693 flybd5, 2020-07-31 02:28
Messages (9)
msg373487 - (view) Author: Juan Jimenez (flybd5) * Date: 2020-07-10 21:44
I have invented a new way to seed the random number generator with about as random a source of seeds as can be found: hashes generated from high cadence, high resolution images of the surface of the Sun. These are captured by the Solar Dynamics Observatory's (SDO) Atmospheric Imaging Assembly's (AIA) cameras at various frequencies. I wrote the POC code in Python and can be seen at https://github.com/flybd5/heliorandom. The HelioViewer project liked the idea and modified their API to do essentially what my POC code does at https://api.helioviewer.org/?action=getRandomSeed. Perhaps a solarseed() call could be created for the library to get seeds that way rather than from the system clock?
msg373488 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-07-10 22:37
Hi, this is very specific and I don't think a way to seed the random generator using a third uncontrolled party will get merged in Python. You should probably try to package this as a third party library.
msg373489 - (view) Author: Juan Jimenez (flybd5) * Date: 2020-07-10 23:42
I'm not a Python guru, but I was wondering, 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?
msg373490 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-11 00:12
> 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
msg373491 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-11 00:14
Marking as closed for these reasons listed by Rémi Lapeyre.

Also note that the default is to use a system source of entropy.  Time is only used as a fallback if such as source is unavailable.
msg373492 - (view) Author: Juan Jimenez (flybd5) * Date: 2020-07-11 00:16
Thanks Rémi. :)
msg373494 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-07-11 00:23
This is definitely cool though. It might make a really sweet example in the demos that come with Python, or in the tutorial, showcasing both the awesomeness of HelioViewer and how to use Python to connect to web APIs.

https://github.com/python/cpython/tree/master/Tools/demo
msg373541 - (view) Author: Juan Jimenez (flybd5) * Date: 2020-07-11 21:51
How would I know if my demo is good enough to be included in that repo? Is there a guide for this, or do I just create a pull request, throw it over the fence and wait until the wolves either grunt in approval or throw it back at me in pieces? I ask because I have never participated in a repo related to as big a project as Python 3. :)
msg374628 - (view) Author: Juan Jimenez (flybd5) * Date: 2020-07-31 02:28
Pull request https://github.com/python/cpython/pull/21693 created for the demo steven.daprano suggested.
History
Date User Action Args
2020-07-31 02:28:01flybd5setmessages: + msg374628
pull_requests: + pull_request20836
2020-07-11 21:51:41flybd5setmessages: + msg373541
2020-07-11 00:23:52steven.dapranosetnosy: + steven.daprano
messages: + msg373494
2020-07-11 00:16:15flybd5setmessages: + msg373492
2020-07-11 00:14:23rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg373491

stage: resolved
2020-07-11 00:12:59rhettingersetnosy: + rhettinger
messages: + msg373490
2020-07-10 23:42:14flybd5setmessages: + msg373489
2020-07-10 22:37:03remi.lapeyresetnosy: + remi.lapeyre
messages: + msg373488
2020-07-10 21:44:43flybd5create