## 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`

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

Try:

``````from random import randrange
print(randrange(10))
``````
2020/06/21
2092
6/21/2020 9:33:40 AM

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

``````random.randint(a, b)
``````

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

2015/06/30

Try this:

``````from random import randrange, uniform

# randrange gives you an integral value
irand = randrange(0, 10)

# uniform gives you a floating-point value
frand = uniform(0, 10)
``````
2010/10/22

``````from random import randint

x = [randint(0, 9) for p in range(0, 10)]
``````

This generates 10 pseudorandom integers in range 0 to 9 inclusive.

2018/05/17

The `secrets` module is new in Python 3.6. This is better than the `random` module for cryptography or security uses.

To randomly print an integer in the inclusive range 0-9:

``````from secrets import randbelow
print(randbelow(10))
``````

For details, see PEP 506.

2016/12/06

Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:

``````import numpy as np
np.random.randint(10, size=(1, 20))
``````

You can expect to see an output of the following form (different random integers will be returned each time you run it; hence you can expect the integers in the output array to differ from the example given below).

``````array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
``````
2017/02/02