1. 2
  1.  

  2. 1

    This should work well in many cases. For a large number of weights, you can replace the linear search with a binary search.

    There’s also a really cool approach called the “alias method”, which you can read about here: https://www.keithschwarz.com/darts-dice-coins/