Monday, January 25, 2021

Andre Roberge: Friendly-contest: we have a winner!

 Since my last post, no new issue has been filed. As the deadline has passed (8 am, AST), I have written a short program to randomly draw a winner. In my last post, I listed incorrectly the entries which I double-checked prior to writing the program, which I tested a few times before the deadline. 

The program I wrote was not the most efficient, but should be easy to understand: I created a list with one item for each valid contest entries, shuffled it and picked the first item on the list as the possible "winner". Just to ensure that I didn't make any silly mistake, I did 100,000 random draws and compared with the original distribution.

The very last of these random draws was determined to be the winner.

Here's the program:

from random import shuffle

entries = {
"Dominik1123": 19,
"sdementen": 6,
"gdementen": 3,
"tomerv": 5,
"dcambie": 3,
"carreau": 1,
}
results = {
"Dominik1123": 0,
"sdementen": 0,
"gdementen": 0,
"tomerv": 0,
"dcambie": 0,
"carreau": 0,
}

tickets = []
for name in entries:
for number in range(entries[name]):
tickets.append(name)

nb_trials = 100_000
rescale = len(tickets) / nb_trials

for i in range(nb_trials):
shuffle(tickets)
results[tickets[0]] += 1

for name in results:
results[name] *= rescale


print("entries:", entries)
print("draws :", results)
print("The winner is:", tickets[0])


And the winner is Dominik1123.


Thanks to every one who filed an issue for the contest, or simply tried Friendly-traceback.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...