// Did we actually get values sent?
$firstVisit = true;
if (count($_POST) > 0) {
$firstVisit = false;
}
$trials = 1000000;
$payout1 = 0;
$chance1 = 50; // editable
$payout2 = 1;
$chance2 = 25; // editable
$payout3 = 2;
$chance3 = 15; // editable
$payout4 = 3;
$chance4 = 9; // editable
$payout5 = 50; // editable
$chance5 = 1;
$alert = "";
if (array_key_exists('chance1', $_POST) && is_numeric($_POST['chance1'])) {
if ( $_POST['chance1'] < 0) {
$alert .= "Alert: Chance of payout 1 was less than 0%.
\n";
}
elseif ($_POST['chance1'] > 100) {
$alert .= "Alert: Chance of payout 1 was greater than 100%.
\n";
}
$chance1 = max(0, $_POST['chance1']);
$chance1 = min($chance1, 100);
}
if (array_key_exists('chance2', $_POST) && is_numeric($_POST['chance2'])) {
if ( $_POST['chance2'] < 0) {
$alert .= "Alert: Chance of payout 2 was less than 0%.
\n";
}
elseif ($_POST['chance2'] > 100) {
$alert .= "Alert: Chance of payout 2 was greater than 100%.
\n";
}
$chance2 = max(0, $_POST['chance2']);
$chance2 = min($chance2, 100);
}
if (array_key_exists('chance3', $_POST) && is_numeric($_POST['chance3'])) {
if ( $_POST['chance3'] < 0) {
$alert .= "Alert: Chance of payout 3 was less than 0%.
\n";
}
elseif ($_POST['chance3'] > 100) {
$alert .= "Alert: Chance of payout 3 was greater than 100%.
\n";
}
$chance3 = max(0, $_POST['chance3']);
$chance3 = min($chance3, 100);
}
if (array_key_exists('chance4', $_POST) && is_numeric($_POST['chance4'])) {
if ( $_POST['chance4'] < 0) {
$alert .= "Alert: Chance of payout 4 was less than 0%.
\n";
}
elseif ($_POST['chance4'] > 100) {
$alert .= "Alert: Chance of payout 4 was greater than 100%.
\n";
}
$chance4 = max(0, $_POST['chance4']);
$chance4 = min($chance4, 100);
}
if (array_key_exists('payout5', $_POST) && is_numeric($_POST['payout5'])) {
$payout5 = max(1, $_POST['payout5']);
}
// confirm that the probabilities add up to 100%
$total = $chance1 + $chance2 + $chance3 + $chance4 + $chance5;
// under 100%
if ($total < 100) {
$alert .= "Alert: Total chance was less than 100%. Increased Payout 1 chance to fix the problem.
\n";
// increase chance1 to match
$chance1 = 100 - ($chance2 + $chance3 + $chance4 + $chance5);
}
// above 100%
elseif ($total > 100) {
$alert .= "Alert: Total chance exceeded 100%. Decreased payouts to fix the problem.
\n";
// decrease chance 1 to match
$chance1 = 100 - ($chance2 + $chance3 + $chance4 + $chance5);
if ($chance1 < 0) {
// decrease chance2 as well
$chance2 += $chance1;
$chance1 = 0;
if ($chance2 < 0) {
$chance3 += $chance2;
$chance2 = 0;
if ($chance3 < 0) {
$chance4 = 100 - $chance5;
$chance3 = 0;
}
}
}
}
$totalProfit = 0;
?>
Puzzle 3
You are an assistant game designer working on a slot machine game.
The lead designer tells you to balance the game so that players
lose 2% of their bet, on average.
Note: Not all of the numbers below are editable.
srand((float)microtime()*1000000);
function randFloat() {
return mt_rand() / mt_getrandmax();
}
if (!$firstVisit) {
for ($t = 0; $t < $trials; $t++) {
// bet 1
$totalProfit--;
$roll = randFloat() * 100;
if ($roll < $chance1) {
$totalProfit += $payout1;
}
elseif ($roll <= $chance1 + $chance2) {
$totalProfit += $payout2;
}
elseif ($roll <= $chance1 + $chance2 + $chance3) {
$totalProfit += $payout3;
}
elseif ($roll <= $chance1 + $chance2 + $chance3 + $chance4) {
$totalProfit += $payout4;
}
elseif ($roll <= $chance1 + $chance2 + $chance3 + $chance4 + $chance5) {
$totalProfit += $payout5;
}
// should never get here
else {
$alert .= "Alert: Some math error. Please e-mail Ira.
\n";
}
}
echo $alert;
echo "After ".$trials." tries, the change in money was ";
echo round($totalProfit/$trials, 3)." bets.
\n";
echo "
\n";
}
?>
Back to Puzzle List