аватар question@mail.ru · 01.01.1970 03:00

How to choose one of the values ​​with a certain probability?

Example. Katya received 10 points, and Kolya & mdash; 20 points. So in one case, Katya will win out of three, and in two of the three & mdash; Kolya.

It is necessary to randomly choose the winner of the lottery, given their probabilities for winning.

What formula is it calculated (a universal formula for any number of lottery participants is needed)?

p.s. Python language.

аватар answer@mail.ru · 01.01.1970 03:00

We will take the condition:

player 1 - the probability of winning 10
player 2 - probability of winning 20

in a simple case, we need to add all the probabilities of the winning together (that is, 10 + 20 in the example) and then generate an accidental number in The range from 0 to probabilities (up to 30). Further, from the resulting random number, to take away the probability of winning in tu (for example, we go from top to bottom), and as soon as the difference becomes less than 0 - you found the desired player (winner).

So, example:

   10  +  20  =  30  // Summary of probabilities ( 30 ) =  26.67  // random number  26.67  -  10  =  16.67  // subtract the probability  1  1  Player  16.67  -  20  = -  3.33  // read the probability  2  the result is less  0.  means  2  the player - the desired winner    

in the English environment The algorithm has a name (and is often used in genetic algorithms). This name is given due to the fact that all probabilities can be represented as a roll wheel sector (in the figure they are shown to the percentage of the amount), and randomly choose a position on the wheel, thereby determining the “winning” “sector.

Latest

Similar