Advertisement

Clever random?

Started by January 02, 2003 11:53 AM
1 comment, last by Halo Vortex 22 years, 1 month ago
Here''s a simple question. I have a set of choices and each choice has a probability in percents. (Alltogether they give 100%). What''s the algo to choose one based on their probability of happening?
Just choose a random 100% ?

For instance (psuedo code, should work)

Choice(1) = 10 (10% probability)Choice(2) = 50 (50% probability)Choice(3) = 40 (40% probability)RandomValue = Random(100) (Random value between 0 and 99)ChoiceIndex = 1Do While Choice(ChoiceIndex) < RandomValue  RandomValue = RandomValue - Choice(ChoiceIndex)  ChoiceIndex = ChoiceIndex + 1Loop 

Advertisement
Thanks.

This topic is closed to new replies.

Advertisement