# Generate random integers between 0 and 9

Advertisement

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

### Accepted Answer

Try:

```
from random import randrange
print(randrange(10))
```

**Docs:** https://docs.python.org/3/library/random.html#random.randrange

2020/06/21

Read more... Read less...

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

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

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

Licensed under: CC-BY-SA with attribution

Not affiliated with: Stack Overflow

Email: [email protected]