weighted probability model?

I'm trying to figure out how to best work a weighted probability model into code - I'm trying to implement a random selection where each element has their own separate probability of being chosen.

I am able to get each probability as a float, and the sum approximates 1.
What is the best way to emulate this sort of weighted probability?

(Sorry if this doesn't belong in the forum; I tried looking it up but didn't really know what I was doing.)
Well, you could try using a bunch of integral types that add up to some predefined max like this:

[0-100 chance stuff][100-150 chance stuff][...]

And then just randomly generate an integer between 0 and the predefined max, giving you the section depending on the number.
I considered doing that, but the number of elements is arbitrary, so wouldn't that end up being wasteful as I need more and more integers in order to accurately mimic the probabilities (as integer ranges)?
Not really. You'd need to store the ranges somehow anyway; I don't think there is a way to avoid it in this case.
Last edited on
Alright, thanks.
Topic archived. No new replies allowed.