Python: Random System time seed
Question:
In python, and assuming I’m on a system which has a random seed generator, how do I get random.seed() to use system time instead? (As if /dev/urandom did not exist)
Answers:
you can do
import random
import time
random.seed(time.time())
import random
from datetime import datetime
random.seed(datetime.now().timestamp())
Do you know this library: PyRandLib? See:
https://schmouk.github.io/PyRandLib/ to easily download archives versions, and
https://github.com/schmouk/PyRandLib to get access to the code.
This library contains many of the best-in-class pseudo-random numbers generators while acting exactly as does the Python “built-in” library random (just un-zip or un-tar the downloaded archive in the ‘Lib/site-packages/’ sub-directory of your Python directory).
From the code, and from module ‘fastrand32.py’, you’ll get a quite more sophisticated way to feed random with a shuffled version of current time. For your purpose, this would become:
import time
import random
t = int( time.time() * 1000.0 )
random.seed( ((t & 0xff000000) >> 24) +
((t & 0x00ff0000) >> 8) +
((t & 0x0000ff00) << 8) +
((t & 0x000000ff) << 24) )
This provides a main advantage: for very short periods of time, the initial seeds for feeding the pseudo-random generator will be hugely different between two successive calls.
As of day, Dec 2021, the better option is to use methods from "secrets" module. You don’t need to set such seed anymore.
Sample Code:
import secrets
print (secrets.randbelow(1_000_000_000))
In python, and assuming I’m on a system which has a random seed generator, how do I get random.seed() to use system time instead? (As if /dev/urandom did not exist)
you can do
import random
import time
random.seed(time.time())
import random
from datetime import datetime
random.seed(datetime.now().timestamp())
Do you know this library: PyRandLib? See:
https://schmouk.github.io/PyRandLib/ to easily download archives versions, and
https://github.com/schmouk/PyRandLib to get access to the code.
This library contains many of the best-in-class pseudo-random numbers generators while acting exactly as does the Python “built-in” library random (just un-zip or un-tar the downloaded archive in the ‘Lib/site-packages/’ sub-directory of your Python directory).
From the code, and from module ‘fastrand32.py’, you’ll get a quite more sophisticated way to feed random with a shuffled version of current time. For your purpose, this would become:
import time
import random
t = int( time.time() * 1000.0 )
random.seed( ((t & 0xff000000) >> 24) +
((t & 0x00ff0000) >> 8) +
((t & 0x0000ff00) << 8) +
((t & 0x000000ff) << 24) )
This provides a main advantage: for very short periods of time, the initial seeds for feeding the pseudo-random generator will be hugely different between two successive calls.
As of day, Dec 2021, the better option is to use methods from "secrets" module. You don’t need to set such seed anymore.
Sample Code:
import secrets
print (secrets.randbelow(1_000_000_000))