Python | Generate random numbers within a given range and store in a list
Last Updated :
23 Apr, 2023
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
import random
def Rand(start, end, num):
res = []
for j in range (num):
res.append(random.randint(start, end))
return res
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
import numpy as np
def Rand(start, end, num):
res = []
for j in range (num):
res.append(np.random.randint(start, end))
return res
num = 10
start = 20
end = 40
print (Rand(start, end, num))
|
Output:
[30, 30, 38, 39, 39, 37, 24, 25, 28, 32]
Using random.sample() function:
Approach:
The easiest and most efficient way to generate random numbers within a given range and store them in a list is to use the random.sample() function. Here’s the algorithm for generating random numbers within a given range and storing them in a list using the random.sample() function:
Import the random module.
Use the random.sample() function to generate a list of unique random numbers within the given range.
Python3
import random
num = 10
start = 20
end = 40
result = random.sample( range (start, end + 1 ), num)
print (result)
|
Output
[26, 33, 21, 40, 23, 39, 34, 38, 36, 32]
Time Complexity: O(n)
Auxiliary Space: O(n)
Using Numpy:
Algorithm:
- Import the numpy module and functools module.
- Define a function Rand() which takes three arguments as input, start, end, and num.
- Create an empty list ‘res’ and iterate num times using a for loop.
- In each iteration, append a randomly generated integer between the start and end range using the numpy module.
- Return the res list.
- Define the values of num, start, and end.
- Use reduce() function to generate the list of random numbers.
- Define a lambda function which takes two arguments, acc and x.
- In each iteration of reduce(), append a randomly generated integer between the start and end range using the numpy module.
- The reduce() function will iterate num times and return the final list of random numbers.
- Print the final result.
Python3
from functools import reduce
import numpy as np
num = 10
start = 20
end = 40
result = reduce ( lambda acc, x: acc + [np.random.randint(start, end)], range (num), [])
print (result)
|
Output:
[27, 36, 21, 24, 34, 29, 31, 31, 22, 33]
Time Complexity: O(n), where n is the value of num.
Space Complexity: O(n), as we are storing n random numbers in the list.
Please Login to comment...