How do you generate a range of random numbers in python?

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:

importrandom

if__name__=='__main__':

    start=2   # inclusive

    end= 5    # inclusive

    n=10      # size

    x= [random.randint(start,end)for _inrange(n)]

    print(x)

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).

importrandom

if__name__=='__main__':

    start=2   # inclusive

    end= 5    # exclusive

    n=10      # size

    x= [random.randrange(start,end)for _inrange(n)]

    print(x)

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.

importrandom

if__name__=='__main__':

    start=2   # inclusive

    end= 5    # exclusive

    n=10      # size

    x= random.choices(range(start,end), k=n)

    print(x)

Download  Run Code

4. Using Numpy

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

numpy.random.randint

importnumpy asnp

if__name__== '__main__':

    start=2   # inclusive

    end=5    # exclusive

    n= 10      # size

    x= np.random.randint(low=start, high=end,size=(n))

    print(x)

numpy.random.uniform

importnumpy asnp

if__name__== '__main__':

    start=2   # inclusive

    end=5    # exclusive

    n= 10

    x= np.random.uniform(low=start, high=end, size=(n)).astype(int)

    print(x)

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


Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding 🙂


How do you generate a range of random values in Python?

Use randrnage() to generate random integer within a range Use a random. randrange() function to get a random integer number from the given exclusive range by specifying the increment. For example, random. randrange(0, 10, 2) will return any random number between 0 and 20 (like 0, 2, 4, 6, 8).

How do you generate a range of random numbers?

Generate a random number with a Max limit.
function generateRandom(maxLimit = 100){.
let rand = Math. random() * maxLimit;.
console. log(rand); // say 99.81321410836433..
rand = Math. floor(rand); // 99..
return rand;.

How do you generate an array of random numbers in Python?

To create an array of random integers in Python with numpy, we use the random. randint() function. Into this random. randint() function, we specify the range of numbers that we want that the random integers can be selected from and how many integers we want.

How do you generate a random number between 1 and 10 in Python?

The randint() method to generates a whole number (integer). You can use randint(0,50) to generate a random number between 0 and 50. To generate random integers between 0 and 9, you can use the function randrange(min,max) . Change the parameters of randint() to generate a number between 1 and 10.