It's still the php probability problem. The probability calculation function is used to solve the problem. I searched many websites for this function, but I didn't understand it. what is the process of solving it ~ This array is the id and probability. Array (???? [1] & nbsp ;=& gt; & nbsp; 1 ???? [2] & nbsp ;=& gt; & nbsp; 5 ???? [3] question about the probability of nb or php, solution description
This is the probability calculation function, which is found on the internet many times, but I have not understood this function. what is the process of solving it ~
This array is the id and probability.
Array
(
????[1] => 1
????[2] => 5
????[3] => 10
????[4] => 12
????[5] => 22
????[6] => 50
)
Why can I get the id of the probability award ?? Get_rand ($ arr );
Function get_rand ($ proArr ){
???? $ Result = '';
???? // The total probability precision of the probability array
???? $ ProSum = array_sum ($ proArr );
???? // Probability array loop
???? Foreach ($ proArr as $ key => $ proCur ){
???????? $ RandNum = mt_rand (1, $ proSum );
???????? If ($ randNum <= $ proCur ){
???????????? $ Result = $ key;
???????????? Break;
????????} Else {
???????????? $ ProSum-= $ proCur;
????????}
????}
???? Unset ($ proArr );
???? Return $ result;
}Share: More