开发者

PHP lottery chances

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 i开发者_如何学JAVAdeea 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:

bob = 1
rob = 2.5
mike = 3.5
john = 4.5
todd = 6

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.


Change you list to

bob - 1 chance 
bob - 1 chance 
rob - 1.5 chance 
rob - 1.5 chance 
rob - 1.5 chance 
mike - 1 chance 
mike - 1 chance 
john - 1 chance 
john - 1 chance 
todd - 1.5 chance 
todd - 1.5 chance 

Then when you select one from this list, some have better or worse chance at winning.


By Changes you probably mean chances. Well, what you can do is make an array, that has the users:

$boughtLotteryTicket = array('bob', 'rob', 'rob', 'mike', 'john', 'todd', 'todd');

And do whatever you wanted to it :). Since you added them twice, they both have double the possibility to win. Same principle when participating in any lottery. Buy more tickets == higher change of winning.


Sum up all the chances weighted with the changes:

$sum =$pSum = 0;
foreach($users as $participant)
    $sum += $participant['change'];

$rand = rand()/get_randmax();
foreach($users as $participant) {
    $pSum += $participant['change']/$sum;
    if($pSum > $rand) {
        $winner = $participant;
        break;
    }
}


This is not likely the best way to handle it but for the sake of getting something this small done I assume for a work related thing..

make a simple array. Of names. Those who have 50% extra of a chance to win I would assume is equal to 2 in 1. So of those names, I would do duplicates in the array.

$lottoNames = array('bob', 'rob', 'rob', 'mike', 'john', 'todd', 'todd');
$x = count($lottoNames)-1;
$lotto = rand(0, $x);
echo $lottoNames[$lotto];

I can't say this would work but its a shawdy idea to run with again for simplicity of it all.


This might work:

$chance = array(
    'bob' => 1,
    'rob' => 1.5,
    'mike' => 1,
    'john' => 1,
    'todd' => 1.5,
);

$range = array_sum($chance);
$offset = rand(0, $range-1);

$i = 0;
foreach ($chance as $person => $weight)
{
    $i += $weight;
    if ($i <= $offset)
    {
        echo $person . ' won';
        break;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜