Reputation: 37
from random import seed,random
for i in range(21):
if i%3==0:
seed(10)
if i%2==0:
random()
else:
random()
with above code, result is
0.5714025946899135
0.4288890546751146
0.5780913011344704
0.5714025946899135
0.4288890546751146
0.5780913011344704
0.5714025946899135
0.4288890546751146
0.5780913011344704
0.5714025946899135
0.4288890546751146
0.5780913011344704
0.5714025946899135
0.4288890546751146
0.5780913011344704
0.5714025946899135
0.4288890546751146
0.5780913011344704
0.5714025946899135
0.4288890546751146
0.5780913011344704
which,
a=0.5714025946899135
b=0.4288890546751146
c=0.5780913011344704
is continuously repeating.
But according to seed(10), i should get only a=0.5714025946899135
with a seed applyed,
and the others should be random.
but why other value (0.4288890546751146
and
0.5780913011344704
) is constant too?
Upvotes: 0
Views: 65
Reputation: 11067
seed(10)
sets the seed and resets the random number generator for that seed. So every 3rd iteration, you're just starting the random numbers (for seed = 10) back to the beginning. If you want different numbers, you need to set the seed to something else (ideally a random number, or leave blank which achieves this last option)
Put it another way, the random numbers that come out of random are deterministic. Given a seed, the output of the random function will be random when looked at as a series, but one that is pre-determined. Every time you pull numbers from a given seed, you'll get the exact same, random numbers, in the same sequence.
Upvotes: 6