samedi 20 février 2021

How to prevent random.sample from generating the same combination of number again?

I just wanted to know if there's a way to stop the random.sample from generating the same combination again in list.

So for example combination [4, 6, 1] already appeared in random.sample then I don't want it to appear again to make the matching of my variable code faster.

Thanks

import random

code = [2, 6, 4]

while True:
    rand = random.sample(range(0, 10), 3)
    if(rand == code):
        print(str(rand) + " access granted")
        break
    else:
        print(str(rand) + "access denied")



Aucun commentaire:

Enregistrer un commentaire