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)
-
2Can't you use something like `random.seed(time.time()`? – fredtantini Dec 03 '14 at 16:04
-
Doesn't Python automatically have a random seed anyways? You're only supposed to provide a seed if you don't want it to be random, I thought? – ArtOfWarfare Dec 03 '14 at 16:24
-
Begs the obvious question: *why* are you trying to deliberately avoid the superior seed in favor of system time? This screams "XY question" to me. – Lee Daniel Crocker Dec 03 '14 at 19:10
-
2long story short, for a ctf competition. It's modeled as a server without os.urandom implemented. At least, I think it is. – Academiphile Dec 04 '14 at 00:12
4 Answers
import random
from datetime import datetime
random.seed(datetime.now().timestamp())

- 1,146
- 12
- 12
-
1For those wondering why this solution is preferred over `time.time()` as a seed, `time.time()` may have only second-level precision on some operating systems. `datetime.now()` creates a seed with [microsecond level detail](https://www.codecademy.com/resources/docs/python/dates/now). – Madhav Malhotra Apr 29 '23 at 14:54
you can do
import random
import time
random.seed(time.time())

- 4,811
- 4
- 30
- 46
-
1from that link:"If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability)." This is what I'm trying to avoid. – Academiphile Dec 03 '14 at 16:02
-
-
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.

- 327
- 2
- 6
-
1Hi Schmouk! I'm not confident with the syntax used for the final expression used for the seed (the argument of `random.seed()` ). Can you explain me how `t` value is modified by that expression? – user1172131 Apr 11 '22 at 11:13
-
1i ran your expression to print five seeds in a loop, several times, and unfortunately the seeds are quite close, at least on a log scale, and usually repeat (i.e., 2/5 are often identical). might need more significant digits from time() and a tweaked formula. – Salmonstrikes Jul 12 '22 at 20:42
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))