Here's how that might look:
Imagine that you're standing at the left end of the strip with a stone. You throw the stone randomly so that it lands somewhere on the strip. If your throw is completely random, it's most likely to land on the 4, right? It might not -- it might land on any of the others -- but it's "weighted" to land on the 4.
This is basically what we're gonna do in code. Here's how you do it:
- Sum up the total weights. In the above example, it'll be 8.
- Pick a random number that's between 0 and the total weight. In the example, this will be a number between 0 and 8.
- Figure out where that random number "lands". Let's say you picked 5.5. Iterate through the array until the total weight up to that point is less than 5.5; this will have you land at the third spot from the left: the 2.
- If you haven't already chosen that space, choose it.
- Repeat steps 2 through 4 until you've chosen them all.
If you used this logic in another language, let me know!