Randomised Ice Cream
For Employee Day, your company is offering everyone free ice cream, and you somehow find yourself at the front of the queue. The vendor manning the ice cream booth is handing out ice creams one by one in alphabetical order of flavour.
You take the first ice cream. When the vendor hands you the second one, you can choose to either:
- Pass the ice cream you are holding to the next employee in line, and take the new ice cream, or
- Hand the new ice cream down to the next employee in line.
This process will continue until all the ice cream is given out, but you don’t know how many flavours there are in total.
You like all flavours equally, so you want all flavours to have an equal chance of being selected. Thankfully, you have a small device in your pocket: you can enter any number X, and it will generate a random number from 1 to X with equal chance.
How can you decide which flavour to keep?
Hint
We don’t know how many flavours there are until the last one has been given out. So after each ice cream has been handed out, your strategy will need you to be holding any particular ice cream with k1 chance, where k is the number of ice creams handed out so far.
Solution
Answer: When you’re handed the kth ice cream, enter k into your device. If it lands on 1, hand your current ice cream to the next employee and takes the new one. Otherwise, pass the new ice cream to the next employee.
The key idea is that your strategy must always act as if the next ice cream could be the last.
Let’s say you are holding the first ice cream, and the second one is handed to you. Your should take the second ice cream with a probability of 21, otherwise you should hold on to the first ice cream. This makes sure that even if there were only 2 ice creams in line, you have gotten them all at equal chance.
As the third ice cream is handed to you, you need to choose it with a probability of 31, and the other 2 ice creams with a probability of 32. And because in the previous step, you would’ve end up with the first and second ice cream at equal chance, the final probability would be 31 for each of them.
Likewise, as the fourth ice cream is handed to you, you need to choose it with a probability 41, and the other 3 ice creams with a probability of 43. And because in the previous step, you would’ve end up with the first, second, and third ice cream at equal chance, the final probability would be 41 for each of them.
In general, when the kth ice cream is handed to you, you need to pick it with a probability of k1. Otherwise, you’ll end up with any of the previous (k−1) ice creams with kk−1 chance in total.
And since in the previous step, you would’ve ended up with any of the previous (k−1) ice creams with a probability of k−11, you’ll end up with any of the earlier ice creams with a probability of kk−1×k−11=k1.
Your device easily lets you generate a k1 probability: simply type k into the device and switch ice cream if the random number is 1.
Try These Next
Measuring Time with Hourglasses 1
You’re making the perfect soft-boiled eggs: all you have to do is boil eggs in a pot of water for exactly 9 minutes. However, your kitchen timer is broken, and...
Chain Link 2
An apprentice is working for you, the village’s master blacksmith, for 21 days. For payment, you have agreed to give him a gold chain with 21 links, one link...
Poisoned Wine (Extension)
Before the king gets to administer the bottles of wine to the prisoners, an advisor stops him. He says that if more than 8 prisoners die, the townspeople might...