Generate random integers between 0 and 9


Question

How can I generate random integers between 0 and 9 (inclusive) in Python?

For example, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

1
1176
10/18/2018 8:08:34 AM

Accepted Answer

Try:

from random import randrange
print(randrange(10))

More info: http://docs.python.org/library/random.html#random.randrange

1863
7/1/2019 12:11:10 PM

import random
print(random.randint(0,9))

random.randint(a, b)

Return a random integer N such that a <= N <= b.

Docs: https://docs.python.org/3.1/library/random.html#random.randint


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon