[Tutor] When I print random.random

Emile van Sebille emile at fenx.com
Sat Jan 15 18:56:56 CET 2011


On 1/14/2011 9:19 PM Steven D'Aprano said...
> walter weston wrote:
>> when I print random.random() it always returns a float why is this?
>> how do I change it to a whole number?
>
> Because random.random() is defined to always return a float between 0
> and 1. That's what it does.
>
> If you want a random whole number, you can call random.randint or
> random.randrange. See the Fine Manual for the difference between them,
> or at the interactive interpreter, call:
>
> help(random.randint)
> help(random.randrange)
>

You can discover that randint and randrange exist interactively by 
typing in dir(random), which is generally a good place to start.  Most 
python libraries are reasonably complete in the functionality they 
provide, and if you want some particular capability, you're not likely 
to be the first so you'll find it's already there.  Once you find names 
that sound like they may provide what you're looking for, use help as 
Steven illustrates above.  If you don't see anything close, use 
help(module_name) and browse the help contents for addition info.

then...

> and read what they say. Then if you still have any questions, ask.
>
>
>




More information about the Tutor mailing list