This post will discuss how to generate n random numbers between the specified range in Python.

1. Using random.randint() function

The random.randint(x, y) function generates the random integer n such that x <= n <= y. You can use it as following to generate random numbers between a certain range:

Download  Run Code

2. Using random.randrange() function

If you need to generate a random number exclusive of the end argument (i.e., x <= n < y), consider using the random.randrange(x, y) function. In other words, random.randrange(x, y+1) is same as the random.randint(x, y).

Download  Run Code

3. Using random.choices() function

Another plausible way to generate random numbers from a continuous sequence is using the random.choice() function.

Download  Run Code

4. Using Numpy

If you prefer Numpy, you can try numpy.random.randint() or numpy.random.uniform() function.

numpy.random.randint

numpy.random.uniform

That’s all about generating random numbers between specified ranges in Python.