Back

Randomised Ice Cream

Medium
Created: February 4, 2026Updated: February 5, 2026

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 XXX, and it will generate a random number from 111 to XXX 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 1k\frac{1}{k}k1 chance, where kkk is the number of ice creams handed out so far.

Solution

Answer: When you’re handed the kthk^{th}kth ice cream, enter kkk into your device. If it lands on 111, 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 12\frac{1}{2}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 13\frac{1}{3}31, and the other 2 ice creams with a probability of 23\frac{2}{3}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 13\frac{1}{3}31 for each of them.

Likewise, as the fourth ice cream is handed to you, you need to choose it with a probability 14\frac{1}{4}41, and the other 3 ice creams with a probability of 34\frac{3}{4}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 14\frac{1}{4}41 for each of them.

In general, when the kthk^{th}kth ice cream is handed to you, you need to pick it with a probability of 1k\frac{1}{k}k1. Otherwise, you’ll end up with any of the previous (k1)(k-1)(k1) ice creams with k1k\frac{k-1}{k}kk1 chance in total.

And since in the previous step, you would’ve ended up with any of the previous (k1)(k-1)(k1) ice creams with a probability of 1k1\frac{1}{k-1}k11, you’ll end up with any of the earlier ice creams with a probability of k1k×1k1=1k\frac{k-1}{k} \times \frac{1}{k-1} = \frac{1}{k}kk1×k11=k1.

Your device easily lets you generate a 1k\frac{1}{k}k1 probability: simply type kkk into the device and switch ice cream if the random number is 1.

Try These Next