
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to generate non-repeating random numbers in Python?
In this article, we will show you how to generate non-repeating random numbers in python. Below are the methods to accomplish this task:
Using randint() & append() functions
Using random.sample() method of given list
Using random.sample() method of a range of numbers
Using random.choices() method
Using randint() & append() functions
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword, to import the random module.
Create an empty list which is the resultant random numbers list.
Use the for loop, to traverse the loop 15 times.
Use the randint() function(Returns a random number within the specified range) of the random module, to generate a random number in the range in specified range i.e, from 1 to 100.
Use the if conditional statement to check whether the generated random number is not in the resultant randomList with the not and in operators.
Use the append() function( adds the element to the list at the end) to append the random number to the resultant list, if the condition is true.
Print the resultant random numbers list
Example
The following program returns the non-repeating random numbers using the randint(), append() functions −
# importing random module import random # resultant random numbers list randomList=[] # traversing the loop 15 times for i in range(15): # generating a random number in the range 1 to 100 r=random.randint(1,100) # checking whether the generated random number is not in the # randomList if r not in randomList: # appending the random number to the resultant list, if the condition is true randomList.append(r) # printing the resultant random numbers list print("non-repeating random numbers are:") print(randomList)
Output
On executing, the above program will generate the following output −
non-repeating random numbers are: [84, 86, 90, 94, 59, 33, 58, 36, 62, 50, 26, 38, 4, 89]
Using random.sample() method of given list
The random.sample() method returns a list containing a randomly selected number of elements from a sequence.
Syntax
random.sample(sequence, k)
Parameters
sequence(required) − any sequence like list, tuple etc
k(optional) − The length of the returned list as an integer
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword, to import the random module.
Create a variable to store an input list.
Use the set() function(returns all the distinct items from an iterable and converts an iterable to set), to remove the repeating elements from the input list.
Use the list() function(converts the sequence/iterable to a list), to convert the above set into a list. Now the list has only unique elements.
Use the sample() function by passing the list containing unique items, k value as arguments to it to print the k(here 4) random numbers from the list which are nonrepeating.
Example
The following program returns the 4 non-repeating random numbers using the random.sample() function −
# importing random module import random # input list inputList = [1, 2, 3, 1, 4, 3, 5, 7, 9, 8, 2, 3] # removing repeating elements from the list using the set() function resultSet=set(inputList) # converting the set into a list(now the list has only unique elements) uniqueList =list(resultSet) # printing 4 random numbers from the list which is non-repeating print("4 non-repeating random numbers from the list are:") print(random.sample(uniqueList, 4))
Output
On executing, the above program will generate the following output −
4 non-repeating random numbers from the list are: [7, 2, 4, 8]
Using random.sample() method of a range of numbers
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword, to import the random module.
Get the numbers in the specified range i.e, here 1 to 100 using the range() function(The range() function returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number).
Use the sample() function by passing the given range of numbers list, k value as arguments to it to print the k(here 4) random numbers from the list which are nonrepeating.
Example
The following program returns the 4 non-repeating random numbers using the random.sample() function −
# importing random module import random # getting numbers from 0 to 100 inputNumbers =range(0,100) # printing 4 random numbers from the given range of numbers which are # non-repeating using sample() function print("4 non-repeating random numbers are:") print(random.sample(inputNumbers, 4))
Output
On executing, the above program will generate the following output −
4 non-repeating random numbers are: [67, 50, 61, 47]
Using random.choices() method
The random module contains the random.choices() method. It is useful to select multiple items from a list or a single item from a specific sequence.
Syntax
random.choices(sequence, k)
Parameters
sequence(required) − any sequence like list, tuple etc
k(optional) − The length of the returned list as an integer
Example
The following program returns the 4 non-repeating random numbers using the random.choices() function −
# importing random module import random # getting numbers from 0 to 100 inputNumbers =range(0,100) # printing 4 random numbers from the given range of numbers which are # non-repeating using choices() function print("4 non-repeating random numbers are:") print(random.choices(inputNumbers, k=4))
Output
On executing, the above program will generate the following output −
4 non-repeating random numbers are: [71, 4, 12, 21]
Conclusion
In this tutorial, we learned how to generate non-repeating numbers in Python using four different ways. We learned how to generate non-repeating numbers that are not on the list. We learned How to determine whether an element is included in a list in order to generate non-repeating integers.
- Related Articles
- How does Python generate random numbers?
- How to use Python Numpy to generate Random Numbers?
- Generate pseudo-random numbers in Python
- Python module to Generate secure random numbers
- How to generate large random numbers in Java?
- How to generate random numbers between two numbers in JavaScript?
- Generate random numbers in Arduino
- How to generate standard normal random numbers in R?
- Guide to Generate Random Numbers in Linux
- Java program to generate random numbers
- How to generate 5 random numbers in MySQL stored procedure?
- Generate Random Integer Numbers in Java
- Generate Secure Random Numbers for Managing Secrets using Python
- How to generate different random numbers in a loop in C++?
- Java Program to generate random numbers string
