R
R
root092016-07-24 14:59:34
PHP
root09, 2016-07-24 14:59:34

How to realize the chance?

How can you realize the chance to perform some action?
For example, there are 3 actions and the following chances are given for them:
1. 20%
2. 40%
3. 60%
How to choose a random action?
There is an idea to add up all the chances, it will turn out 20 + 40 + 60 = 120, then through rand choose a random value from 120, and which number will fall out - then the action will be. But if there is any action with a 100% chance to stand, then this method will not work ... tell me how best to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
index0h, 2016-07-24
@root09

You are pointing percentages in vain, this is not correct. 20, 40, 60 are just hit probability multipliers. So even if there are 20, 40, 60, 100 - this will not be a problem. Count rand(0, 20+40+60+100), then determine what limits the number falls into.

L
Lander, 2016-07-24
@usdglander

Is the event group complete or are the events independent?
For independent events:

if (rand(0,100) < 20) 
{ 
  /*Выполняем событие 1*/
}
if (rand(0,100) < 40) 
{ 
  /*Выполняем событие 2*/
}
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question