Build a prefix sum array from the weights. For w = [1, 3], the prefix sums are [1, 4]. The total weight is .
To pick an index, generate a random number from to the total weight. Then binary search the prefix sum array for the smallest index whose prefix sum is greater than or equal to your random number.
Why does this work? Each index "owns" a range proportional to its weight. Index owns range [1, 1] ( value). Index owns range [2, 4] ( values). The random number lands in each range with the correct probability.