PHP • Creating a number range from an array

Status
Not open for further replies.

brsy

nah mang
May 12, 2011
1,530
272
So basically, when someone enters $1.00 into a raffle, they get a ticket per cent entered, in an ascending order.
  • So if user 1 enters $4.31, user 2 enters $1.09, and user 3 enters $6, there would be a total of 1,140 tickets in the raffle.
  • User 1 would have ticket numbers 1 through 431, because he entered the first 431 tickets into the raffle, and user 2 would have tickets 432 through 540, because he entered the next 109 tickets, and user 3 would have tickets 542 through 1,140.
I made a function that would convert the dollars to total number of tickets, and display how many tickets the user has, along with their chance of winning. I then used the rand() function to select a random ticket number from the total number of tickets. Obviously, user 3 would have the greatest chance (a bit over 50%) of winning because he has 600 of the 1,140 tickets in the raffle.

How can I check which user the winning ticket belongs to?

PHP:
<?php

    $user = array('$4.31', '$1.09', '$6.00');
    $oldTotal = 0;

    foreach($user as $number) {

        $number = str_replace('$', '', $number);
        $number = str_replace('.', '', $number);
        settype($number, "integer");

        $total = $number + $oldTotal;
        $oldTotal = $total;
    }

    foreach($user as $blessed) {
        $blessed = str_replace('$', '', $blessed);
        $blessed = str_replace('.', '', $blessed);

        echo $blessed . " tickets – " . round($blessed / $total * 100, 2) ."% chance<br>";
    }
    echo "<br>Total Tickets: ".$total . "<br>";
    echo "Winning Ticket: ". rand(1, $total);
?>
 
Last edited by a moderator:

Adil

DevBest CEO
May 28, 2011
1,278
716
Use a tuple to store the user ID and ticket number together
(I'll update this post in about 15 minutes)

PHP:
<?php

   
    $user = array(array(1,'$4.31'), array(2,'$1.09'), array(3, '$6.00'));
    $oldTotal = 0;

    foreach($user as $userPair) {

        $number = str_replace('$', '', $userPair[0]);
        $number = str_replace('.', '', $userPair[0]);
        settype($userPair, "integer");

        $total = $number + $oldTotal;
        $oldTotal = $total;
    }

    foreach($user as $blessed) {
        $blessed = str_replace('$', '', $blessed[1]);
        $blessed = str_replace('.', '', $blessed)[1];

        echo $blessed . " tickets – " . round($blessed / $total * 100, 2) ."% chance<br>";
    }
    echo "<br>Total Tickets: ".$total . "<br>";
    echo "Winning Ticket: ". rand(1, $total);
?>

Arrays can act like tuples so I created an array of arrays (with two values).
 
Last edited:

RastaLulz

fight teh power
Staff member
May 3, 2010
3,934
3,932
Here's how I'd do it:
PHP:
<?php

$totalTickets = 0;

$users[] = ['amount' => '$4.30'];

$users[] = ['amount' => '$5.70'];

$users[] = ['amount' => '$10.00'];

foreach($users as $userId => $userInfo)
{
    $ticketsBought = str_replace(['$', '.'], '', $userInfo['amount']);

    $minTicket = $totalTickets + 1;
    $maxTicket = $totalTickets + $ticketsBought;

    $users[$userId]['tickets'] = ['min' => $minTicket, 'max' => $maxTicket];

    $totalTickets = $ticketsBought + $totalTickets;

    echo "User {$userId} has bought tickets {$minTicket} through {$maxTicket}.<br>";
}

$ticket = rand(1, $totalTickets);

foreach($users as $userId => $userInfo)
{
    if($userInfo['tickets']['min'] <= $ticket && $userInfo['tickets']['max'] >= $ticket)
    {
        echo "User {$userId} has won with ticket {$ticket} (owning tickets {$userInfo['tickets']['min']} through {$userInfo['tickets']['max']}).";
    }
}
 
Status
Not open for further replies.

Users who are viewing this thread

Top