Skip to content
Related Articles

Related Articles

Python | Generate random numbers within a given range and store in a list

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 03 Nov, 2022
Improve Article
Save Article

Given lower and upper limits, Generate random numbers list in Python within a given range, starting from ‘start’ to ‘end’, and store them in the list. Here, we will generate any random number in Python using different methods.

Examples:

Input: num = 10, start = 20, end = 40
Output: [23, 20, 30, 33, 30, 36, 37, 27, 28, 38]
Explanation: The output contains 10 random numbers in range [20, 40]

Input: num = 5, start = 10, end = 15
Output: [15, 11, 15, 12, 11]
Explanation: The output contains 5 random numbers in range [10, 15]

Method 1: Generate random integers using random.randrange() method

Python provides a function named randrange() in the random package that can produce random numbers from a given range while still enabling spaces for steps to be included. The below example uses randrange() to randomly print integers.

Python3




import random
 
 
print("Random integers between 0 and 9: ")
for i in range(4, 15):
     y = random.randrange(9)
     print(y)


Output:

Random integers between 0 and 9: 
4
7
2
8
4
6
2
3
1
5
3

Method 2: Generate random integers using random.uniform() method

In python, there’s an inbuilt method, “random.uniform()” which performs this task with ease and uses just one word. This method is defined in the “random” module. It Returns the generated floating-point random number between the lower limit and upper limit. 

Python3




import random
 
 
print("Random integers between 0 and 9: ")
for i in range(4, 11):
     y = random.uniform(4, 10)
     print(y)


Output:

Random integers between 0 and 9: 
7.157267168334274
7.924883261968617
5.353672487638509
9.769791404923588
5.511960438487713
8.116097767143245
7.5873695577485165

Method 3: Generate random integers using randbelow() method

For handling crucial information including cryptographically secure passwords, account authentication, security tokens, and related secrets, the secrets module is utilized to generate random integers. We can use randbelow() function from the secrets module to generate random integers. The below example uses randbelow() to randomly print integers.

Python3




from secrets import randbelow
 
for _ in range(3, 9):
 
    print(randbelow(15))


Output:

14
7
13
10
9
7

Method 4: Generate random integers using the random.randint() method

Python provides a random module to generate random numbers. To generate random numbers we have used the random function along with the use of the random.randint function. randint accepts two parameters, a starting point, and an ending point. Both should be integers and the first value should always be less than the second.

Python3




# Python code to generate
# random numbers and
# append them to a list
import random
 
# Function to generate
# and append them
# start = starting range,
# end = ending range
# num = number of
# elements needs to be appended
def Rand(start, end, num):
    res = []
 
    for j in range(num):
        res.append(random.randint(start, end))
 
    return res
 
# Driver Code
num = 10
start = 20
end = 40
print(Rand(start, end, num))


Output: 

[23, 20, 30, 33, 30, 36, 37, 27, 28, 38]

Method 5:  Using the NumPy random.randint() method to generate random numbers

The random module in Python 3 includes a built-in function called randint() in Python. The random module provides access to many useful functions, one of which is randint, which can produce random numbers.  The below example uses randint() to randomly print integers.

Python3




# Python code to generate
# random numbers and
# append them to a list
import numpy as np
def Rand(start, end, num):
    res = []
 
    for j in range(num):
        res.append(np.random.randint(start, end))
 
    return res
 
 
# Driver Code
num = 10
start = 20
end = 40
print(Rand(start, end, num))


Output:

[30, 30, 38, 39, 39, 37, 24, 25, 28, 32]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!