[Tutor] about random seed

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Sep 3 19:50:34 CEST 2006


>> random.seed() sets the starting number for the generator. Setting the 
>> seed to a known value can be important if you want the same sequence of 
>> pseudorandom numbers to be generated each time you test/run your 
>> program.
>>
> I still can not understand. can you show me an example?

Hi Linda,

Ok, let's start from basics.


Normally, functions give the same results if we pass in the same inputs. 
If we have a function like:

################
def square(x):
     return x * x
################

then we really expect 'square(42)' to return the same value as 
'square(42)' because the input is the same.


But this poses a dilemma: we'd like to have a function that gives us 
"random" numbers, but we also want to be able to call it using the same 
(empty) input.  That is, we'd like:

     random.random()

to give a different result than another call to:

     random.random()

In the mathematical sense, random.random() isn't a "function", but that's 
ok, because we programmers play fast and loose with these things anyway. 
*grin* So how does this work?


The idea is to have the random.random() function keep some memory of the 
last random number that it already returned.  That way, when we call 
random.random() again, it'll have a chance to return something different. 
The idea looks like:

###################################
_hidden_seed = 0
def my_random():
     global _hidden_seed
     _hidden_seed = _hidden_seed + 1
     return _hidden_seed
###################################

I'm putting in the understored '_hidden_seed' global variable that's 
reused in our calls to my_random().


Now my_random() will give us varying results every time we call it:

######
>>> my_random()
1
>>> my_random()
2
>>> my_random()
3
######


But the only problem here, now, is that the results aren't particularly 
"random" looking.  So maybe we can do something a little crazier besides 
just adding 1 to it: maybe we can do some multiplication, take remainders, 
... etc, to scramble the number up.  That's the job of a good random 
number generator.


Also notice that there's nothing truly "random" doing on here.  The stream 
of numbers that come out of mutiple calls to my_random() is completely 
predictable if we know two things:

      * the initial seeding value

      * the algorithm used to generate the next value

Out of these two, the only thing that's potentially different between 
Python runs is the seeding value, since the algorithm we use is fixed. 
When Python starts up, it's initially set to some value that relates to 
the current time, to further extend the illusion of randomness between 
program runs.

random.seed(), the function you were trying to play with, resets the seed 
to something you want.  So if you want to forcefully generate the same 
"random" values, set the seed to something hardcoded, and then start 
calling random.random().


Please feel free to ask more questions about this.


More information about the Tutor mailing list