Given the upper limit and lower limit, the task is to generate n natural numbers which are not repeating in Python.
Examples:
Example1:
Input:
Given N=13 Given lower limit range =19 Given upper limit range =45
Output:
The random numbers present in the range from 19 to 45 are : 28 40 24 25 20 44 38 29 21 31 43
Example2:
Input:
Given N=19 Given lower limit range =23 Given upper limit range =41
Output:
The random numbers present in the range from 23 to 41 are : 26 27 40 38 37 41 30 35 36 23 25
Random choice of Random module in Python with no Repeat
Below are the ways to generate n natural numbers which are not repeating in Python.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Method #1: Using For Loop and randint function (Static Input)
Approach:
- Import the random module using the import keyword.
- Give the number n as static input and store it in a variable.
- Give the lower limit range and upper limit range as static input and store them in two separate variables.
- Take an empty list (say rndmnumbs) and initialize it with an empty list using [] or list().
- Loop till n times using For loop.
- Generate a random number using randint(lowerlimitrange,upperlimitrange) and store it in a variable.
- Check whether the above random number is present in the list or not using not in operator.
- If it is not in the list then append the element to the rndmnumbs list using the append() function.
- Print the rndmnumbs.
- The Exit of the Program.
Below is the implementation:
# Import the random module using the import keyword.
import random
# Give the number n as static input and store it in a variable.
numbe = 13
# Give the lower limit range and upper limit range as static input
# and store them in two separate variables.
lowerlimitrange = 19
upperlimitrange = 45
# Take an empty list (say rndmnumbs) and initialize it with an empty list
# using [] or list().
rndmnumbs = []
# Loop till n times using For loop.
for m in range(numbe):
# Generate a random number using randint(lowerlimitrange,upperlimitrange)
# and store it in a variable.
randomnumbe = random.randint(lowerlimitrange, upperlimitrange)
# Check whether the above random number is present in the list or not
# using not in operator.
if randomnumbe not in rndmnumbs:
# If it is not in the list then append the element
# to the rndmnumbs list using the append() function.
rndmnumbs.append(randomnumbe)
# Print the rndmnumbs
print('The random numbers present in the range from',
lowerlimitrange, 'to', upperlimitrange, 'are :')
for q in rndmnumbs:
print(q, end=' ')
Output:
The random numbers present in the range from 19 to 45 are : 28 40 24 25 20 44 38 29 21 31 43
Method #2: Using For Loop and randint function (User Input)
Approach:
- Import the random module using the import keyword.
- Give the number n as user input using int(input()) and store it in a variable.
- Give the lower limit range and upper limit range as user input using map(),int(),split(),input() functions.
- Store them in two separate variables.
- Take an empty list (say rndmnumbs) and initialize it with an empty list using [] or list().
- Loop till n times using For loop.
- Generate a random number using randint(lowerlimitrange,upperlimitrange) and store it in a variable.
- Check whether the above random number is present in the list or not using not in operator.
- If it is not in the list then append the element to the rndmnumbs list using the append() function.
- Print the rndmnumbs.
- The Exit of the Program.
Below is the implementation:
# Import the random module using the import keyword.
import random
# Give the number n as user input using int(input()) and store it in a variable.
numbe = int(input('Enter some random number = '))
# Give the lower limit range and upper limit range as user input
# using map(),int(),split(),input() functions.
# Store them in two separate variables.
lowerlimitrange = int(input('Enter random lower limit range = '))
upperlimitrange = int(input('Enter random upper limit range = '))
# Take an empty list (say rndmnumbs) and initialize it with an empty list
# using [] or list().
rndmnumbs = []
# Loop till n times using For loop.
for m in range(numbe):
# Generate a random number using randint(lowerlimitrange,upperlimitrange)
# and store it in a variable.
randomnumbe = random.randint(lowerlimitrange, upperlimitrange)
# Check whether the above random number is present in the list or not
# using not in operator.
if randomnumbe not in rndmnumbs:
# If it is not in the list then append the element
# to the rndmnumbs list using the append() function.
rndmnumbs.append(randomnumbe)
# Print the rndmnumbs
print('The random numbers present in the range from',
lowerlimitrange, 'to', upperlimitrange, 'are :')
for q in rndmnumbs:
print(q, end=' ')
Output:
Enter some random number = 19 Enter random lower limit range = 23 Enter random upper limit range = 41 The random numbers present in the range from 23 to 41 are : 26 27 40 38 37 41 30 35 36 23 25
Related Programs:
- python program to remove the ith occurrence of the given word in a list where words can repeat
- create numpy array of different shapes initialize with identical values using numpy full in python
- python program to print each word of a sentence along with number of vowels in each word
- how to web scrape with python in 4 minutes
- how to create and initialize a list of lists in python
- python how to find all indexes of an item in a list
- basics of python built in types
