[Tutor] Random Numbers

John Fouhy john at fouhy.net
Sun Nov 20 23:35:53 CET 2005


On 21/11/05, Merrie <merries at comcast.net> wrote:
> Ok first thing I need a direction on is random number generation, I looked
> through the Python manual and I must be missing it.
>
> What Im looking for, needing is a random generation 1 through 10 + modifier,
> example  like rolling a 1d10 +1

Hi Merrie,

The manual section you want is the random module:
http://docs.python.org/lib/module-random.html

In particular, the randrange() function will help you out here. 
random.randrange() is equivalent to generating a list using the
builtin function range(), and then choosing one element from that list
at random.

For example, I can generate some random numbers in the range 0..9 like this:

>>> import random
>>> for i in range(10):
...  print random.randrange(10)
...
8
2
0
9
7
5
8
0
7
9
>>>

HTH!

--
John.


More information about the Tutor mailing list