Can someone explain what the method seed does from module random in the following example? Thanks in advance.
random.seed(42) #same connections each time for easier debugging
Can someone explain what the method seed does from module random in the following example? Thanks in advance.
random.seed(42) #same connections each time for easier debugging
The random function is pseudo random, not real random. It produces seemingly random values from a seeded value, normally the seeded value is set by the system clock or some other changing value that would make more sense, but if the random function is seeded by the same static value (42 in this case) the output from the random function will be exactly the same on each program execution and thus predictable, which can be good for debugging code.
Any software-based random number generator isn't truly random unless it pulls random data from hardware sources.
Seeding a random number generator (RNG) provides it with an initial value. The RNG does some magic to this value and produces a new value, which is fed into it again (usually) and then this produces another value. This keeps occurring over and over to create a ton of very random-looking numbers. Real RNGs aren't that simple, but you get the idea.
Since the RNG isn't actually random (it's actually a PRNG, where P stands for pseudo), feeding it the same seed value twice will give you the same stream of random numbers each time. This is only useful for debugging as you know what the "random" numbers will be.