6 minutes ago, Kylotan said:The code wrongly uses the relative frequencies in the array rather than the cumulative frequencies, so that it's possible to generate a random value that will never be less than or equal to any of the values in the array.
And the iteration should be forwards, not backwards. Currently you'd exit the loop on the first iteration every time (if the previous bug was fixed).
Once those are fixed, this would be a reasonable implementation of roulette wheel selection.
The code uses relative frequencies (1000, 1000, 1000, 3000). It would be more clear if he actually wrote that and then some code that did the accumulation, or if he had stuck to the original example so we know for sure what he means.
The iteration seems fine to me.