title: Algorithm to distribute according to weights, with unknown total of items, guaranteeing good distribution?
body: <p>I want to distribute tokens into 3 slots.</p>
  
  <p>Each slot has some weight: maybe 50% of tokens should go into the first slot, 30% should go into the second slot, 20% into the third slot. </p>
  
  <p>I don't know the total number of tokens – they keep on coming. I may get 1000 tokens to distribute at noon, I get another 300 at 1 p.m. and so on, unpredictably. At any point in time, the tokens I have received so far should be distributed as well as possible according to the weights.</p>
  
  <p>One solution is to distribute by probabilities. I roll a 100-sided die for each token. If the result is 1–50, the token goes in slot 1. A result of 51–80 mean slot 2, a result of 81–100 mean slot 3.</p>
  
  <p>But this means that it's not impossible (only improbable) that every single token ends up in slot 3, for example.</p>
  
  <p>I want to guarantee that when I've received a total of 100 tokens, then exactly 50 of them will be in slot 1. When I've received 1000 tokens, exactly 500 should be in slot 1.</p>
  
  <p>What is a good algorithm for this?</p>