I am using the following code to seed for my RNG. The results just look too close together to be statistically reasonable (edge cases appearing in succession far too often).
This RNG does not have to be perfect, but I would like it to be reasonable. Would sleeping the entire routine a second (or less), between iterations improve the seeding? Time is not really a big deal. If the entire program runs in 20 seconds or 2 seconds is not an issue.
seed_clock = time.time()
seed(seed_clock)
value = randint(1, 100)
CodePudding user response:
The funny thing with randomness is that you can get two very similar results using different seeds it is just not very likely. Delaying seconds won't help you:
The seed "primes" the random number generator - each way to pull a random number from it mutates the internal state in a predictable manner so you get the same "numbers" from it if you use the same seed and same order of actions.
import random
random.seed(42)
print( random.getrandbits(20))
print( random.getrandbits(30))
print( random.getrandbits(40))
random.seed(42)
print( random.getrandbits(20))
print( random.getrandbits(40))
print( random.getrandbits(30))
random.seed(42)
print( random.getrandbits(40))
print( random.getrandbits(30))
print( random.getrandbits(20))
Outputs for 20,30,40 randbits in different orders:
670487 # 20 based off of seed(42)
119540831 # 30
811856239313 # 40
670487 # 20 same based off of seed(42)
26247967103 # 40 different
796233790 # 30 different
123005401501 # 40 based off of seed(42)
26855092 # 30 different
777572 # 20 different
You only seed if you want predictability and you only get predictability if you use the exact same sequence of state-mutating actions - any other "similarities" are just that - randomness.
If you want somehow "more" randomness - you could switch to some cryptographically used randomness generator - like https://docs.python.org/3/library/secrets.html but even with that you may get things that seem not very random to your eye.
This Williams - Why Are People Bad at Detecting Randomness? A Statistical Argument or this Why are People Bad at Detecting Randomness? Because it is Hard. may be an interesting read - as are
Humans are biased. A number of studies so far confirm that humans are not a good source of randomness. A couple to get you started:
W. A. Wagenaar (1972). "Generation of random sequences by human subjects: a critical survey of the literature". Psychological Bulletin 77: p65–72
Brugger, P. (1997). Variables that influence the generation of random sequences: An update. Perceptual and Motor Skills, 84(2), 627-661.
Persaud, N. (2005). Humans can consciously generate random number sequences: A possible test for artificial intelligence. Medical hypotheses, 65(2), 211-214
(Source: answer on reddit - thread: https://www.reddit.com/r/askscience/comments/3nm2kp/comment/cvpx3ol/?utm_source=share&utm_medium=web2x&context=3)