Last updated: Apr 10, 2024
Reading timeยท6 min
To generate N unique random numbers within a range:
range()
class to create a range
object.random.sample()
method to get a list of N unique random numbers.random.sample()
method returns a list of N unique elements from the
provided sequence.import random def gen_random_numbers_in_range(low, high, n): return random.sample(range(low, high), n) # ๐๏ธ [6, 9, 4, 8, 1] print(gen_random_numbers_in_range(1, 10, 5)) # ๐๏ธ [8, 4, 1, 3, 5] print(gen_random_numbers_in_range(1, 10, 5))
We used the range()
class to get a range
object.
print(list(range(1, 5))) # ๐๏ธ [1, 2, 3, 4] print(list(range(1, 8))) # ๐๏ธ [1, 2, 3, 4, 5, 6, 7]
The range() class is commonly used for looping a specific number of times.
The range()
class takes the following arguments:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range will consist of every N numbers from start to stop (defaults to 1 ) |
The random.sample() method returns a list of N unique elements chosen from the provided sequence.
import random # ๐๏ธ [5, 3, 8, 4, 7] print( random.sample( range(1, 10), 5 ) )
The random.sample()
method returns an N-sized list of elements chosen from the
sequence without replacement.
The random.sample()
method raises a ValueError
if the sample is larger than
the sequence.
import random # โ๏ธ ValueError print( random.sample( range(1, 5), 5 ) )
If you need to handle this scenario, use a try/except block.
import random try: random_numbers = random.sample( range(1, 5), 5 ) except ValueError: # ๐๏ธ this runs print('The sample is larger than the sequence')
Trying to retrieve more elements than are present in the range
caused a
ValueError
which was then handled by the except
block.
Alternatively, you can use the random.shuffle()
method.
This is a four-step process:
range()
class to get a range
object.list()
class to convert the range
object to a list.random.shuffle()
method to shuffle the list.import random def gen_random_numbers_in_range(low, high, n): a_list = list(range(low, high)) random.shuffle(a_list) return a_list[:n] # ๐๏ธ [5, 7, 9, 3, 6] print(gen_random_numbers_in_range(1, 10, 5)) # ๐๏ธ [9, 4, 6, 3, 8] print(gen_random_numbers_in_range(1, 10, 5))
The random.shuffle() method takes a sequence and shuffles it in place.
import random a_list = list(range(1, 10)) random.shuffle(a_list) print(a_list) # ๐๏ธ [4, 7, 3, 9, 1, 2, 8, 6, 5]
The last step is to use list slicing to select N unique random elements from the list.
The syntax for list slicing is my_list[start:stop:step]
.
The start
index is inclusive and the stop
index is exclusive (up to, but not
including).
start
index is omitted, it is considered to be 0
, if the stop
index is omitted, the slice goes to the end of the list.Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(my_list) - 1
.
The slice list[:n]
returns the first n
elements of the shuffled list.
This is a three-step process:
range()
class to create a range
object.numpy.random.choice()
method to create a list of numbers without
duplicates.replace
argument to False
to select unique numbers.import numpy as np list_of_numbers = np.random.choice( range(1, 15), 6, replace=False ).tolist() print(list_of_numbers) # ๐๏ธ [13, 14, 9, 6, 11, 12]
Make sure you have the NumPy module installed to be able to run the code sample.
pip install numpy # ๐๏ธ or with pip3 pip3 install numpy
The numpy.random.choice method generates a random sample from the given array-like object.
replace
argument determines whether a value of the array-like object can be selected multiple times. The argument defaults to True
.We set the replace
argument to False
, so no duplicates are selected.
You can use the tolist() method if you need to convert the NumPy array to a native Python list.
To generate a random number in a range, excluding some numbers:
range
object.not in
operator to exclude the numbers from the list.random.choice()
method to generate a random number in the range.from random import choice def gen_random_number(low, high, exclude): return choice( [number for number in range(low, high) if number not in exclude] ) numbers_to_exclude = [1, 3, 7] # ๐๏ธ 2 print(gen_random_number(1, 10, numbers_to_exclude)) # ๐๏ธ 19 print(gen_random_number(1, 100, numbers_to_exclude))
We used a
list comprehension to
iterate over a range
object.
The range() class is commonly used for looping a specific number of times.
print(list(range(1, 5))) # ๐๏ธ [1, 2, 3, 4] print(list(range(1, 3))) # ๐๏ธ [1, 2]
On each iteration, we use the not in
operator to exclude a list of numbers
from the result.
from random import choice def gen_random_number(low, high, exclude): return choice( [number for number in range(low, high) if number not in exclude] )
The in operator tests
for membership. For example, x in l
evaluates to True
if x
is a member of
l
, otherwise, it evaluates to False
.
x not in l
returns the negation of x in l
.The last step is to use the random.choice()
method.
The random.choice method takes a sequence and returns a random element from the non-empty sequence.
import random print(random.choice(['a', 'b'])) # ๐๏ธ "a"
If the sequence is empty, the method raises an IndexError
.
from random import choice def gen_random_number(low, high, exclude): return choice( [number for number in range(low, high) if number not in exclude] ) numbers_to_exclude = [1, 3, 7] result = [ gen_random_number(1, 10, numbers_to_exclude) for _ in range(3) ] print(result) # ๐๏ธ [5, 2, 5]
We used a list comprehension to iterate over a range
object of length N and
called the get_random_number()
function on each iteration.
Alternatively, you can use set
objects.
This is a four-step process:
set
containing all numbers in the range.set
object.set
objects.random.choice()
method to generate a random number in the range.from random import choice def gen_random_number(low, high, exclude): return choice( list( set(number for number in range(low, high)) - set(exclude) ) ) numbers_to_exclude = [1, 3, 7] print(gen_random_number(1, 10, numbers_to_exclude)) # ๐๏ธ 4 result = [ gen_random_number(1, 10, numbers_to_exclude) for _ in range(3) ] print(result) # ๐๏ธ [5, 2, 5]
We used the set() class
to convert the range
object and the list of numbers to exclude to set
objects.
difference()
method.The minus sign is a shorthand for calling the difference()
method on the
set
.
print({1, 2, 3} - {2, 3}) # ๐๏ธ {1}
The
difference()
method returns a new set
with elements in the set
that are not in the
provided iterable.
We used the list() class to convert the
set
object to a list and used the random.choice()
method to pick a random
number from the list.
You can learn more about the related topics by checking out the following tutorials: