Generate Unique Random Numbers in a Range using Python

range & random.sample functions

import random


def generateRandomNumbers(minValue, maxValue, qty):
    numbers = range(minValue, maxValue + 1)

    return random.sample(numbers, qty)


numbers = generateRandomNumbers(1, 10, 4)
print(numbers)  # [1, 9, 5, 7]

Leave a Comment

Cancel reply

Your email address will not be published.