Creating a random integer....

Matthew Thorley ruach at chpc.utah.edu
Fri Nov 7 11:53:23 EST 2003


Python has a module for doing just that its called random. Try this:

import random

# print a random interger between 0 100
print random.randint(0,100)

# print a random number from the list lst
lst = [1,3,6,17,87,330]
print random.choice(lst)

Hope this helps. I recommend picking up a copy of Python Standard 
Library by Fredrick Lundh at your local book store or library. It will 
have more detailed information about the random module and the PSL.

-matthew

Code_Dark wrote:
> Hi, sorry to bore you with my newbie questions, but I _am_ a newbie
> so.. right. Anyway, I was working out a "number guessing game", which
> works fine, except I wanted to make the number a random number so it's
> different every time... can you please tell me what to do to get
> python to create a random number?
> 
> Thanks,
> 
> - Code Dark





More information about the Python-list mailing list