Write a Python program to generate a series of unique random numbers


The program generates a list of integers from 0 to 49 using the range() function and stores them in the list variable ch. Then it uses the random.shuffle() function to randomly shuffle the elements in the list. The program then removes the last element from the shuffled list using the pop() method and prints it using the print() function.

Next, the program enters a loop that prompts the user whether they want another random number. If the user inputs 'n' or 'N', the loop will break, and the program will terminate. If the user inputs 'y' or 'Y', the program will remove the last element from the shuffled list using the pop() method and print it using the print() function.

The loop continues until the user inputs 'n' or until there are no more elements left in the list. Overall, the program generates and prints a random number from the shuffled list, and then allows the user to request more random numbers until they choose to stop.

Source Code

import random
ch = list(range(50))
random.shuffle(ch)
print(ch.pop())
while ch:
    if input("Do You Want Another Random Number?(Y/N) :" ).lower() == 'n':
        break
    print(ch.pop())

Output

46
Do You Want Another Random Number?(Y/N) :y
4
Do You Want Another Random Number?(Y/N) :n

Example Programs