[Tutor] Random Numbers

Gregor Lingl glingl@aon.at
Thu Jul 24 12:55:02 2003


Michie DeBerry schrieb:

>Hello,
>Allow me to start by introducing myself. My name is Michie, and I am currently 
>reading Sam's Teach Yourself Python in 24 Hours. I was wondering if there is 
>anyway to create a random number in Python. I am currently writting a program 
>to create a password for you. It will have more features later on, but for 
>right now, that is all I need it to do. Any suggestions as how to go about 
>creating a random number x number of times to get a-z or 0-9 from a list?
>  
>
Many things about "random" you find in the module random. Docs at:

http://www.python.org/doc/current/lib/module-random.html

random integers you  may create with the function randint:

 >>> from random import randint
 >>> for i in range(25):
    print randint(1,7),

   
1 5 4 1 4 3 4 6 1 4 7 2 2 3 4 2 3 6 1 1 5 2 4 3 1

For your purpose the function choice, which selects random elements
from a sequence, may be better suited:

 >>> from random import choice
 >>> for i in range(25):
    print choice("abcdefghijklmnopqrstuvwxyz"),

   
u f l v y g f e e y s g j d i e v v q d b i l h q
 >>>

HTH, Gregor

>Thx,
>Michie (pronounced Mickey)
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>