How to create a list of random numbers in a given range in Python
In this tutorial, you will learn how to create a list of random numbers in a given range in Python.
Python has a set of functions to generate multiple random numbers. These functions are available in a module called random module in Python.
To generate random numbers we use the function called randint().
randint() accepts two parameters, start and end
Python program to create a list of random numbers in a given range
# Python code to create a list of random numbers in a given range. import random # Function to create and append random numbers in a list. def rand(start, end, rang): result =  for j in range(rang): result.append(random.randint(start, end)) return result # Driver Code if __name__== '__main__': rang = 10 start = 10 end = 50 print(rand(start, end, rang))
#[ list of any 10 random numbers ] [34, 13, 21, 23, 23, 26, 24, 16, 41, 22]
We have created a function called rand() which takes 3 arguments ie start, end and rang. start and end store the values of numbers between which we want to generate the random numbers whereas rang stores the value of how many numbers to be stored.
start and end are used as a parameter for the randint function whereas rang is in the for loop. The variable result is an empty list that stores the generated random numbers and appends function inserts the elements to the end of the list.