I am a bit confused on what random.seed()
does in Python. For example, why does the below trials do what they do (consistently)?
>>> i
A random number is generated by some operation on previous value.
If there is no previous value then the current time is taken as previous value automatically. We can provide this previous value by own using random.seed(x)
where x
could be any number or string etc.
Hence random.random()
is not actually perfect random number, it could be predicted via random.seed(x)
.
import random
random.seed(45) #seed=45
random.random() #1st rand value=0.2718754143840908
0.2718754143840908
random.random() #2nd rand value=0.48802820785090784
0.48802820785090784
random.seed(45) # again reasign seed=45
random.random()
0.2718754143840908 #matching with 1st rand value
random.random()
0.48802820785090784 #matching with 2nd rand value
Hence, generating a random number is not actually random, because it runs on algorithms. Algorithms always give the same output based on the same input. This means, it depends on the value of the seed. So, in order to make it more random, time is automatically assigned to seed()
.
Pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value.
Seeding a pseudo-random number generator gives it its first "previous" value. Each seed value will correspond to a sequence of generated values for a given random number generator. That is, if you provide the same seed twice, you get the same sequence of numbers twice.
Generally, you want to seed your random number generator with some value that will change each execution of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.
Imho, it is used to generate same random course result when you use random.seed(samedigit)
again.
In [47]: random.randint(7,10)
Out[47]: 9
In [48]: random.randint(7,10)
Out[48]: 9
In [49]: random.randint(7,10)
Out[49]: 7
In [50]: random.randint(7,10)
Out[50]: 10
In [51]: random.seed(5)
In [52]: random.randint(7,10)
Out[52]: 9
In [53]: random.seed(5)
In [54]: random.randint(7,10)
Out[54]: 9
random.seed(a, version)
in python is used to initialize the pseudo-random number generator (PRNG).
PRNG is algorithm that generates sequence of numbers approximating the properties of random numbers. These random numbers can be reproduced using the seed value. So, if you provide seed value, PRNG starts from an arbitrary starting state using a seed.
Argument a
is the seed value. If the a value is None
, then by default, current system time is used.
and version
is An integer specifying how to convert the a parameter into a integer. Default value is 2.
import random
random.seed(9001)
random.randint(1, 10) #this gives output of 1
# 1
If you want the same random number to be reproduced then provide the same seed again
random.seed(9001)
random.randint(1, 10) # this will give the same output of 1
# 1
If you don't provide the seed, then it generate different number and not 1 as before
random.randint(1, 10) # this gives 7 without providing seed
# 7
If you provide different seed than before, then it will give you a different random number
random.seed(9002)
random.randint(1, 10) # this gives you 5 not 1
# 5
So, in summary, if you want the same random number to be reproduced, provide the seed. Specifically, the same seed.
# Simple Python program to understand random.seed() importance
import random
random.seed(10)
for i in range(5):
print(random.randint(1, 100))
Execute the above program multiple times...
1st attempt: prints 5 random integers in the range of 1 - 100
2nd attempt: prints same 5 random numbers appeared in the above execution.
3rd attempt: same
.....So on
Explanation: Every time we are running the above program we are setting seed to 10, then random generator takes this as a reference variable. And then by doing some predefined formula, it generates a random number.
Hence setting seed to 10 in the next execution again sets reference number to 10 and again the same behavior starts...
As soon as we reset the seed value it gives the same plants.
Note: Change the seed value and run the program, you'll see a different random sequence than the previous one.
>>> random.seed(9001)
>>> random.randint(1, 10)
1
>>> random.seed(9001)
>>> random.randint(1, 10)
1
>>> random.seed(9001)
>>> random.randint(1, 10)
1
>>> random.seed(9001)
>>> random.randint(1, 10)
1
>>> random.seed(9002)
>>> random.randint(1, 10)
3
You try this.
Let's say 'random.seed' gives a value to random value generator ('random.randint()') which generates these values on the basis of this seed. One of the must properties of random numbers is that they should be reproducible. When you put same seed, you get the same pattern of random numbers. This way you are generating them right from the start. You give a different seed- it starts with a different initial (above 3).
Given a seed, it will generate random numbers between 1 and 10 one after another. So you assume one set of numbers for one seed value.