I will list the usernames in the order I receive them and use the modulus of 123456789 to pick the winner.
names[123456789 % len(names)]
I got fully nerd-sniped trying to work out if there was any issue picking a random choice like this.
tl;dr math nerding feel free to ignore 😅
One issue is modulo bias, which can cause lower values to occur slightly more frequently than higher values when picking a number from a range of numbers that isn't a multiple of the modulus.
The other issue is that picking a particular fixed number can have weird issues modulo n
.
For example, consider a highly composite number like 367567200
that is divisible by many numbers:
>>> [367567200 % n for n in range(1, 31)]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 14, 0, 0, 0, 0, 0, 1, 0]
Ticket #1 (index 0) is going to almost always win!
The other extreme would be a prime number like 179426491
that is not divisible by many numbers:
>>> [179426491 % n for n in range(1,31)]
[0, 1, 1, 3, 1, 1, 6, 3, 7, 1, 2, 7, 10, 13, 1, 11, 8, 7, 10, 11, 13, 13, 18, 19, 16, 23, 16, 27, 11, 1]
Now the only way for the Ticket #1 (index 0) to win is if no one else participates!
123456789
only has a couple of prime factors (3 3 41152263 41152263
), but that's enough to know that the only way for Ticket #1 (index 0) to win is if 1, 3, 9 people enter the raffle (assuming you don't hit 41 million+ entries). 😜
picking random choices from a list
One way to avoid these issues would be to use Python's random.choice
function to pick a random value from the names
list (you can also use random.seed(123456789)
if you want pre-seed the RNG with a reproducible value):
>>> import random
>>> random.choice(names)
'ducky'
That said, without accurately knowing the current number of entries, it would be tricky to game a one-off raffle like this one.