dimanche 11 février 2018

Randomly selecting N number of items from a list, duplicates possible

I am trying to generate a list of random items from another list b. Duplicates are allowed. I cannot use random.sample because N can exceed the number of items in list b.
I have written some code below:

def generate_random_sequence(n):
   population = []
   for i in xrange(n):
       b = random.choice(stuff)
       population.append(b) 

However i am really concerned about it's performance as it will be performed a lot of times. Is there a method in Random library that performs this task? Or is there a more optimized way of doing this task?




Aucun commentaire:

Enregistrer un commentaire