Apex › Storage & databases

The Alias method — O(1) weighted sampling

Draw from a weighted distribution in constant time. The setup chops the n weights into n equal columns, each holding at most two outcomes (a primary and an alias); a sample is then one die roll to pick a column and one coin flip to pick within it. Drag the weights and watch the columns rebalance while every outcome keeps exactly its intended probability.

More in Storage & databases