Building an array for weighted random selection

Here’s a little ICE tree that takes an array like
[ 0.1, 0.2, 0.2, 0.5 ]
and builds an array where

• 10% of the elements have value 0
• 20% of the elements have value 1
• 20% of the elements have value 2
• 50% of the elements have value 3

This is something I wanted to do for randomizing with weighted probabilities. The array [ 0.1, 0.2, 0.2, 0.5 ] is the weights I want to use for the values 0, 1, 2, and 3. The idea is that if I randomly select from the larger array, the weights will determine how likely I am to get each value (eg if 50% of the elements have value 3, then I’ll be much more likely to get that value when I randomly select).

To automatically build the array, I had to use a Repeat with Counter; I didn’t see around that.

So, for example, in an array of 1000 elements, I would have 100 elements with the value 0, 200 elements with the value 1, 200 elements with the value 2, and 500 elements with the value 3.

That works fine if the input array of weights adds up to 1, but what if I start with an array like [2 4 2 8 16] ?
To handle that, I need to make a small change: