Hello everyone.
I want to randomize what action should be taken. Each option should have a defined percentage of chance to happen. And it should be easy to change the percentages.
Let's say that I have 3 options, I then want to use option 1 30% of the time, option 2 30% of the time and option 3 40% of the time. After a change option 1 will happen 15% of the time, option 2 25% of the time and option 3 60% of the time. Of course, in the live application it is not 3 options, it is 14 options now and will probably be more in the future.
I know it is possible to use if-statements or select-statements more or less like the pseudocode below, but since it is hard to change it is not an ideal solution for me.
// randomize a variable named $percent that may be between 0 and 99
if ($percent < 30) {
// option 1
} elseif ($percent < 60) {
// option 2
} else {
// option 3
}
Can anyone give me a suggestion about how I could solve this problem?