The seed function of the random module python

Saicharan papani
3 min readMay 26, 2020
seed() python

I always have a curiosity about guessing things whose result is random (like, a dice result).

My first thought for such type of problem in real life will be too crazy and quixotic:

  • If we have at most precision in our eyesight(like, able to see the moments of dice in slow motion and all that cinematic stuff), then we can guess the result by tracking the physical moments of it.

Now trying to find some practical solution for this:

  • The only thing that I can conclude for the randomness of dice is that we cannot track the moments and decode physics laws on which it is possessing that behavior and the result. so we have to find some intermediatory that does our work, which should be able to understand the behavior, pattern, and predict the output.
  • We opted for the intermediatory to be a computer. As we all know that we have to instruct every single instruction to it else it does nothing. The only thing they provide for us is the fastness and meticulous nature of their work. Now let’s leverage their strength.
  • Starting programming and tell the system the sequence of steps that it has to follow(program). And build an algorithm.
  • We can opt for the ML, AI, and build some interesting model by studying the behavior, the pattern of dice by keeping a fixed measured surrounding it. we can consider inputs like the “angle” at which it is hitting the ground, “force” that we apply on dice to roll, etc. This will require a lot of research for the approach itself. For now, let’s keep this perspective and approach on hold because this article is not for that. we are here for the seed() function of the random module.
  • The random module of python gives us the functions to find random things like a random number, etc.

Do you really think the random number selected is random?

  • Definitely, No.
  • In real life random results are obtained due to some hindrances(conditions), based on these hindrances it tunes itself and eventually produces some result. The obtained result is assumed as random as we could not track the path or algorithm through which it obtained this result.
  • In a similar pattern the random module functions create a simulation by abstracting the process of obtaining the random value. Then the user thinks that he/she is getting the random value.
  • The random module is also not purely random, It is based on some internal algorithms that will eventually repeat the result after some time.
  • Now Take a look at the following code(generates the value between 0 and 1):

>>> from random import random
>>> for i in range(5):
print(random())

0.6691601027899978
0.7253552270718704
0.22527978450828345
0.10419542912335433
0.22453031170295712

  • When we run this. A seed value is initialized at the start of the program and determines the order in which the generated values will appear.

Now, What is a seed?

  • A random number generator takes a value called a seed, treats it as an input value, calculates a “random” number based on it (the method depends on a chosen algorithm), and produces a new seed value.
  • The length of a cycle in which all seed values are unique may be very long, but it isn’t infinite — sooner or later the seed values will start repeating, and the generating values will repeat, too. This is normal. It’s a feature, not a mistake, or a bug.
  • The random factor of the process may be augmented by setting the seed with a number taken from the current time — this may ensure that each program launch will start from a different seed value (ergo, it will use different random numbers).
  • Fortunately, such initialization is done by Python during module import.
  • From this concept, It is pretty clear that the randomness is achieved by seed value it is picked from the current time. so it dupes and makes us believe that it is random.
  • Copy-paste the following code and run it.

>>> from random import random, seed
>>> seed(0)
>>> for i in range(5):
print(random())

0.8444218515250481
0.7579544029403025
0.420571580830845
0.25891675029296335
0.5112747213686085

  • This will give you the same result as mine in every system with some minor precision mismatch because you have initialized the same seed value as mine.

--

--

Saicharan papani

I am a Full-stack developer who has good knowledge of DSA and algorithms. Loves to code.