mercredi 21 octobre 2020

Best way to generate list of random integers with replacement

I am able to generate a list of random integers with replacement using a for loop, but it seems verbose and there must be a more elegant way of doing this with either pure python or using random without a loop. Are there more optimal solutions that would produce the outcome rand_list below without appending on every loop?

import random

rand_list = [] 

for x in range(100):

    r = random.choice([1,2,3,4,5])
    rand_list.append(r)



Aucun commentaire:

Enregistrer un commentaire