2

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
2
  • 2
    Here's a link to the python documentation on the function you are asking about: link. I always go to the documentation if I want to know something about python, it's very useful. Commented Jul 15, 2012 at 5:26
  • You can also look at the Python source code for the random module. Commented Jul 15, 2012 at 15:47

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.