<?php
function scene()
{
$set = array('volvo', 'mici', 'pici');
$scene = array();
$n = count($set);
for ($i = 0; $i < 3; $i++) {
$j = rand(0, $n-1);
$scene[$i] = $set[$j];
for ($i_ = $j; $i_ < $n - 1; $i_++)
$set[$i_] = $set[$i_ + 1];
$n--;
}
return $scene;
}
function denier_wins()
{
$scene = scene();
$n = count($scene);
$chosen = rand(0, $n - 1);
return $scene[$chosen] == 'volvo';
}
function denier_statistics()
{
$n = 0;
for ($i = 0; $i < 3000; $i++)
if (denier_wins())
$n++;
return $n / 3000;
}
function which_is_volvo($scene)
{
$n = count($scene);
for ($i = 0; $i < $n; $i++)
if ($scene[$i] == 'volvo')
return $i;
}
function which_shown($scene, $chosen)
{
$n = count($scene);
$volvo = which_is_volvo($scene);
if ($volvo == $chosen) {
$show = rand(0, $n - 1 - 1);
if ($show < $volvo)
return $show;
else
return $show + 1;
} else {
$show = rand(0, $n - 1 - 2);
if ($show < min($chosen, $volvo))
return $show;
if ($show == min($chosen, $volvo))
$show++;
if ($show == max($chosen, $volvo))
$show++;
return $show;
}
}
function other($scene, $chosen, $shown)
{
$n = count($scene);
$other = rand(0, $n - 1 - 2);
if ($other < min($chosen, $shown))
return $other;
if ($other == min($chosen, $shown))
$other++;
if ($other == max($chosen, $shown))
$other++;
return $other;
}
function accepter_wins()
{
$scene = scene();
$n = count($scene);
$chosen = rand(0, $n - 1);
$shown = which_shown($scene, $chosen);
$other = other($scene, $chosen, $shown);
return $scene[$other] == 'volvo';
}
function accepter_statistics()
{
$n = 0;
for ($i = 0; $i < 3000; $i++)
if (accepter_wins())
$n++;
return $n / 3000;
}
$scene = scene();
var_dump($scene);
$n = count($scene);
echo "Denier statistics: " . denier_statistics()."\n";
echo "Accepter statistics: " .accepter_statistics()."\n";
echo "\n";
echo "A trial sample:\n";
echo "===============\n";
echo 'Volvo: '.which_is_volvo($scene)."\n";
$chosen = rand(0, 2);
echo "Chosen: $chosen\n";
$shown = which_shown($scene, $chosen);
echo "Shown: $shown\n";
$other = other($scene, $chosen, $shown);
echo "Other: $other\n";