vendredi 28 août 2020

Picking unique random numbers from a range [duplicate]

I am trying to pick 3 unique random numbers, all have to be different from 5. I am currently using the following code:

numbers = []
while len(numbers) < 3:
    num = random.choice([i for i in range(10) if i != 5])
    if num not in numbers:
        numbers.append(num)

Usually, this code works, but sometimes I get less than 3 numbers in the numbers list. Can you help me find the problem with the code, or suggest a new code that works?




Aucun commentaire:

Enregistrer un commentaire