title: Data structures for loaded dice?
body: <p>Suppose that I have an <em>n</em>-sided loaded die, where each side <em>k</em> has some probability <em>p</em><sub><em>k</em></sub> of coming up when I roll it. I’m curious if there is a good data structure for storing this information statically (i.e., for a fixed set of probabilities), so that I can efficiently simulate a random roll of the die.</p>
  <p>Currently, I have an O(lg <em>n</em>) solution for this problem. The idea is to store a table of the cumulative probability of the first <em>k</em> sides for all <em>k</em>, then generate a random real number in the range [0, 1) and perform a binary search over the table to get the largest index whose cumulative value is no greater than the chosen value.</p>
  <p>I rather like this solution, but it seems odd that the runtime doesn’t take the probabilities into account. In particular, in the extreme cases of one side always coming up or the values being uniformly distributed, it’s possible to generate the result of the roll in O(1) using a naive approach, while my solution will still take logarithmically many steps.</p>
  <p>Does anyone have any suggestions for how to solve this problem in a way that is somehow “adaptive” in it’s runtime?</p>
  <p><strong>Update:</strong> Based on the answers to this question, I have written up <strong><a href="http://www.keithschwarz.com/darts-dice-coins/" rel="nofollow noreferrer">an article describing many approaches to this problem</a></strong>, along with their analyses. It looks like Vose’s implementation of the alias method gives Θ(<em>n</em>) preprocessing time and O(1) time per die roll, which is truly impressive.  Hopefully this is a useful addition to the information contained in the answers!</p>