[Tutor] how to print random number multiply

Corey Richardson kb1pkl at aim.com
Sat Jan 15 01:57:50 CET 2011


On 01/14/2011 07:46 PM, walter weston wrote:
> I have mostly studied python and now I'm ready to start writing code. I
> want to print random numbers a certain ammount of times I am using the
> code      
> 
>  import random
> print (random.random())
> 
> I tried bind the print statement to a variable and when I call x for
> example I want it to print new numbers when I store the print statement
> in a variable x everytime I call x it's the same number and does change
> I wanted it to change when I call it could someone show me the code to
> do that thanks!
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
When you do:
x = print(random.random())
that evaluates random.random() once, and not every time you type in x.
What you want it is for loop:

for number in range(50):
    print(random.random())

Coming from other languages you might think:
cntr = 0
while cntr < 50:
    print(random.random())
    cntr += 1

which is correct, but not pythonic.

HTH,
~Corey Richardson


More information about the Tutor mailing list