I am working on a lottery script where from a list of 5 users a random one is choosed. My problem is that some of the users must have 50% more chances to win.
For example
bob - 1 chance
rob - 1.5 chance
mike - 1 chance
john - 1 chance
todd - 1.5 chance
How can i make this work? I was thinking build an array and use array_rand to get a random user but i have no ideea how to distribute the chances.
Thanks
For an uneven distribution like this, the approach is as follows:
Add all the weightings together, to get a total. This is the figure that you would use as a random number ceiling. In your example, this would be 6.
Now build an array with each element having the sum of all the elements below it in the array (the sort order doesn’t matter).
Thus you would have an array like this:
Now you can get the random number, and pick the array element which is the highest one where the score is less than the random number.
This will give you your weighted randomness, regardless of how uneven the weightings are.