[Tutor] about random seed

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Sun Sep 3 10:02:30 CEST 2006


linda.s schreef:
> I still can not understand. can you show me an example?
> Thanks!
> Linda

Example program:

import random
random.seed(42)
for i in range(10):
     print random.random()


At the start of the program, the random number generator is seeded with 
42. This could be any number, but the point is that it doesn't change 
when you run the program again. As a result, the output is the same 
every time you run the program.

If you choose a different seed, you get different results from 
random.random().

If you don't use random.seed(), the random module uses the current time 
as the seed. Since the current time always changes, the program will 
generate different output each time it runs.


-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven



More information about the Tutor mailing list