In: Computer Science
Write a Python function that will ask the user for an integer X bigger than 20 and return a list "ls" of 10 distinct random integers taken between the interval 0, to X. None of the 10 elements of "ls" should be repeated.
As per your requirement, here is the Python code:
Rawcode:
#importing random module
import random
#function which generates the distinct random integers taken between the interval 0, to X
def randomList(X):
#creating an empty list
ls = []
#loop which runs 10 times to generate 10 random numbers
for i in range(0, 10):
#loop which runs until the random number which is not there in list
while(1):
#generating random number between 0 to X
n = random.randint(0, X)
#if the generated number is already in list
if n in ls:
#then again generating the number
n = random.randint(0, X)
#if the generated number is not there in list
else:
#appending that number to the list
ls.append(n)
#and breaking the while loop
break
#return the list
return ls
#asking user to enter the integer greater than 20
X = int(input("Enter integer greater than 20: "))
#checking if given value is less than 20
if(X < 20):
#printing that please enter the integer greater than 20
print("Please enter integer greater than 20.")
else:
#calling function which generates 10 distinct random elements in given range
print(randomList(X))
Here is the screenshot of source code:
Here
is the screenshot of output:
Hope this helps you. Please feel free to ask if you still have any queries.
Do upvote. Thank you !