Python program to generate random numbers within a given range and store in a list?

PythonProgrammingServer Side Programming

Here 3 user input variable is used, one for starting number, other one is ending number and last one how many random number want to display. Python provides a random module to generate random numbers. Here we use random function along with radiant function.

Example

n :: 10
s :: 20
e :: 40
[20, 35, 32, 27, 24, 35, 28, 31, 20, 28]

Algorithm

Randomno(s,e,n)
Step 1 : input starting range(s), ending range(e) and number of elements needs to be appended(n).
Step 2 : r= []                            /*Python provides a random module to generate random numbers.
Step 3 : for j in range(n)	
   r.append(random.radient(s,e))         /*radiant is a method, it accepts two parameters.          

Example Code

# To generate random numbers and store in a list
import random
def randomno(s,e,n):
   ran = []
   for i in range(n):
      ran.append(random.randint(s, e))
   return ran
# Driver Code
n = int(input("How many Random Numbers want to disaply ::"))
s = int(input("Enter Starting number ::"))
e = int(input("Enter Ending number ::"))
print(randomno(s, e, n))

Output

How many Random Numbers want to disaply :: 10
Enter Starting number :: 20
Enter Ending number :: 40
[20, 35, 32, 27, 24, 35, 28, 31, 20, 28]
raja
Published on 25-Sep-2018 12:22:12
Advertisements