Can't Generate Random #

Fredrik Lundh fredrik at effbot.org
Mon Feb 5 14:57:09 EST 2001


jimmypop181 at my-deja.com wrote:
> Does somebody know how to generate a random number in
> Python?  I read the docs and it says rand(x, y) should do it.

what docs?  the rand module disappeared from the
language years ago...

use the "random" module instead:

    >>> import random
    >>> random.random()
    0.78562250893885366
    >>> random.randrange(0, 10)
    4

> This also happens when trying to get the Square Root.

you mean "sqrt"?  did you import the "math" module first?
did you use the full name of the function (math.sqrt)?

    >>> import math
    >>> math.sqrt(2)
    1.4142135623730951

(btw, have you read the tutorial at www.python.org?)

Cheers /F





More information about the Python-list mailing list