I have a for loop where at each turn I choose a random number from a constant range. However, I never want to get a duplicate value. This is how I am doing it now:
import random
my_range = 1000
chosen = [81, 944, 576, 618, 333, 350, 579, 774, 86, 511, 619, 552, 804, 44, 894, 408, 242]
for i in range(500):
rand_ind = random.randint(0,1000)
while rand_ind in chosen:
rand_ind = random.randint(0,my_range)
chosen.append(rand_ind)
print(chosen)
Is there any way to do this in one line or more efficiently?
Aucun commentaire:
Enregistrer un commentaire