Friday, September 18, 2009

code for monty hall problem in perl

#! /usr/bin/perl -w
# file: experiment3.pl
use strict;

my $i = 0;
my $j = 0;
my $k = 0;
print "number of trials: \n";
my $trials = ;

for ($k; $k<$trials; $k++)
{
my $car = int(rand 3);
my $choice = int(rand 3);
if ($car == $choice)
{
my $option = int(rand 1);
# $option = 1 is considered as not switching
# $option = 0 is considered as switching
if ($option == 1)
{
$i++;
}
else
{
$j++;
}
}
else
{
my $options = int(rand 1);
if ($options == 1)
{
$j++;
}
else
{
$i++;
}
}
}
print "you won the car in $i trials\n";
print "you lost the car in $j trials\n";
exit 0;