Can't Generate Random #

Alex Martelli aleaxit at yahoo.com
Mon Feb 5 14:57:51 EST 2001


<jimmypop181 at my-deja.com> wrote in message
news:95mu0p$aqc$1 at nnrp1.deja.com...
> Does somebody know how to generate a random number in
> Python?  I read the docs and it says rand(x, y) should do it.  But

D:\MinGw32>python
Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import random
>>> random.random()
0.56534112337917264
>>> for i in range(10): print random.randint(1,7),
...
6 3 5 4 2 1 3 2 1 4
>>>

> whenever I try that, It says that "there is no variable named rand".
> This also happens when trying to get the Square Root.  Please

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

In brief: you must import the module containing the stuff you
need, in general.  Some functions are 'built-in' (no import is
needed), but most are in appropriate modules.


Alex







More information about the Python-list mailing list