dimanche 7 avril 2019

Run a random algorithm mutiple times and average over the results

I have the following random selection script:

import random

length_of_list = 200
my_list = list(range(length_of_list))
num_selections = 10

numbers = random.sample(my_list, num_selections)

It looks at a list of predetermined size and randomly selects 10 numbers. Is there a way to run this section 500 times and then get the top 10 numbers which were selected the most? I was thinking that I could feed the numbers into a dictionary and then get the top 10 numbers from there. So far, I've done the following:

for run in range(0, 500):
    numbers = random.sample(my_list, num_selections)
    for number in numbers:
        current_number = my_dict.get(number)
        key_number = number
        my_dict.update(number = number+1)

    print(my_dict)

Here I want the code to take the current number assigned to that key and then add 1, but I cannot manage to make it work. It seems like the key for the dictionary update has to be that specific key, cannot insert a variable.. Also I think having this nested loop might not be so efficient as I have to run this 500 times 1500 times 23... so I am concerned about performance. If anyone has an idea of what I should try, it would be great! Thanks




Aucun commentaire:

Enregistrer un commentaire