There is an array with indices from 1 to 9. Each index is a chance multiplied by 10 (that is, index 1 is 10% chance, 2 is 20% and up to 9 = 90%) that this index will be chosen, the task is to determine the index.
I came to this solution: Create an array in which the number of values will be equal to the index number with the index number. Example:
arr[1] = [1] arr[2] = [2] arr[3] = [2] arr[4] = [3] arr[5] = [3] arr[6] = [3] arr[7] = [4] arr[8] = [4] arr[9] = [4] arr[10] = [4] ... arr[..] = [7] //не считал какой именно получается номер индекса arr[..] = [7] arr[..] = [7] arr[..] = [7] arr[..] = [7] arr[..] = [7] ... Next, make the suffle of the array (though in fact it is not necessary).
Next do $ rand = rand (1, count (arr))
And the result will be arr [$ rand] - the value will be the array index from 1 to 9 ...
The question is: did I understand everything correctly, and if so, what alternatives are simpler ...
ADDITION FOR NOT UNDERSTANDING THE TASK:
There are 9 doors in front of you, each door has a number 1, 2, 3, 4 ... 9. The chance that you go in the door with the number 1 = 10%, the chance that you go in the door with the number 2 = 20%, the chance that you go in the door with the number 7 = 70% and so on to the door with the number 9 which has a 90% chance that you will go there. Which door will you enter?
As option number 2
// если не из 45, а из 10 $randNum = rand(1, 90) if($randNum = 1 or $randNum = 2) return 1 // 2% elseif($randNum >= 3 and $randNum <= 6) return 2 // 4% elseif($randNum >= 7 and $randNum <= 12) return 3 // 6% elseif($randNum >= 13 and $randNum <= 20) return 4 // 8% elseif($randNum >= 21 and $randNum <= 30) return 5 // 10% elseif($randNum >= 31 and $randNum <= 42) return 6 // 12% elseif($randNum >= 43 and $randNum <= 56) return 7 // 14% elseif($randNum >= 57 and $randNum <= 72) return 8 // 16% elseif($randNum >= 73 and $randNum <= 90) return 9 // 18% //90% This is all! figured out ...
В мешке находится 1 шар с цифрой 1, 2 шара с цифрой 2, 3 шара с цифрой 3 .... 9 шаров с цифрой 9. Какова вероятность вытащить шар с каждой цифрой?- MasterAlex