PDA

View Full Version : [SOLVED] generating a random number in python

crazyfuturamanoob
March 11th, 2008, 01:24 PM
I want to give a random number,
that is smaller than 10 to a variable.

Thanks.

WW
March 11th, 2008, 01:28 PM
Take a look at the random module. Try this:

>>> import random
>>> help(random)

or look here (http://docs.python.org/lib/module-random.html).

Do you want integers or floating point numbers?

crazyfuturamanoob
March 11th, 2008, 01:35 PM
Integers.
But im curious and
want to know how to
get floating point numbers too.

Wybiral
March 11th, 2008, 01:46 PM
The "random" module has all kinds of generators (integer and float as well as different distributions).

WW
March 11th, 2008, 01:47 PM
For integers, use randrange:

>>> import random
>>> random.randrange(10)
8
>>> random.randrange(10)
0
>>> random.randrange(10)
1
>>>

For floating point numbers, the basic function is random, which creates numbers in [0,1):

>>> random.random()
0.90300988531330029
>>> random.random()
0.50559672165513858
>>> random.random()
0.73444115443236646
>>>

crazyfuturamanoob
March 11th, 2008, 01:52 PM
For integers, use randrange:

>>> import random
>>> random.randrange(10)
8
>>> random.randrange(10)
0
>>> random.randrange(10)
1
>>>

Thank, but you forgot to put the rest 2
arguments to randrange (start,end,step).

WW
March 11th, 2008, 01:59 PM
Those arguments are optional. Fire up python and try it out:

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> random.randrange(10)
9
>>> random.randrange(10)
5
>>> random.randrange(10)
5
>>> range(100,125,5)
[100, 105, 110, 115, 120]
>>> random.randrange(100,125,5)
105
>>> random.randrange(100,125,5)
120
>>> random.randrange(100,125,5)
110
>>> random.randrange(100,125,5)
105
>>>