0

I have a system where I can have multiple choices with multiple possible percentages/probabilities. Eg

A - 20%
B - 30%
C - 50%

I want to choose one of them based on the probability. I can think of how to do it where I build ranges for each such as

A = 0-20
B = 20-50
C = 50-100

But it seems messy that I have to build a range for B based on the values that came before in generating the other parts of the range.

I'm wondering if there is a more elegant way to do this?

*I'm programming this in PHP - extra points for code!

Yehosef
  • 17,987
  • 7
  • 35
  • 56
  • 2
    http://stackoverflow.com/questions/12571578/random-value-from-array-by-weight-in-php – Jens W Nov 17 '14 at 12:31
  • thanks @JensW - the answer I wanted was http://stackoverflow.com/a/12571681/214545 The other answers are more memory/cpu intensive I think – Yehosef Nov 17 '14 at 12:42

0 Answers0