w e l c o m e . t o . t r o g ' s . h a u s
image display:




Viewing projects/DealOrNoDeal/deal_or_no_deal.phps

<?
/*
 * A quick script to emulate runnings of Deal or No Deal's box selection stuff
  */
$number_of_times_last_two = 0;
$number_of_times_switching_won = 0;
$number_of_times_switching_lost = 0;

function runGame()
{
    global $number_of_times_last_two;
    global $number_of_times_switching_won;
    global $number_of_times_switching_lost;
    
    $boxes = array();

    // Loop through 26 boxes  and fill them with stuff, and put 200,000 in the last box. 
    for ($i = 0; $i <= 25; $i++)
    {
        if ($i == 25)
            $boxes[] = 200000;
        else
            $boxes[] = $i*10;
    }

    // Shuffle the order, before I discovered shuffle()
    $box_open_order = array_rand($boxes, 26);

    // Open the boxes in the random order
    for ($i = 0; $i <= 25; $i++)
    {
        $opened_box_idx = $box_open_order[$i];
        $opened_box = $boxes[$opened_box_idx];

        // If we're in the last 2 boxes...
        if ($i == 24 || $i == 25)
        {
            // and the box being opened is the $200,000...
            if ($opened_box == 200000)
            {
                $number_of_times_last_two++;
                // if it's the 2nd last box...
                if ($i == 24)
                {
                    // ...switch and see if we win
                    if ($boxes[25] == 200000)
                        $number_of_times_switching_won++;
                    // ...otherwise, we lose
                    else
                        $number_of_times_switching_lost++;
                }            
                else if ($i == 25) // and repeat above 
                {
                    if ($boxes[24] == 200000)
                        $number_of_times_switching_won++;
                    else
                        $number_of_times_switching_lost++;
                }
            }
        }
    }
}

// How many loops do we want to do?
$num_loops = 10000;
for ($i = 0; $i < $num_loops; $i++)
    runGame();
    
print "Number of times 200,000 appeared in the final 2: $number_of_times_last_two (".($number_of_times_last_two/$num_loops).")\n";
print "Number of times switching won: $number_of_times_switching_won\n";
print "Number of times switching lost: $number_of_times_switching_lost\n";
    
/*
 * Sample output from 10,000 loops:
 * Number of times 200,000 appeared in the final 2: 781 (0.0781)
 * Number of times switching won: 383
 * Number of times switching lost: 398
 */
?>


index of root >> projects >> DealOrNoDeal >> deal_or_no_deal.phps >>


deal_or_no_deal.phps
2,006



Last modified: