I forgot I still had this... This program is an experiment for the 3 doors problem. Here is the problem :
Behind a door there is a prize.
You select a door.
The host opens a door that does not have the prize behind it and asks if you would like to change your choice to the other door.
Should you switch? Does it make a difference?
#!/usr/bin/perl -w
srand($$|time);
$matches = '0';
@Set = qw(1 2 3);
$count = '1000000';
populate();
for ($i=0;$i<$count;$i++) {
$temp = int(rand($#Set+1));
$guess = $Set[$temp];
if ($Test[$i] == $guess) {
$matches++;
}
}
print("Trial 1 successes = $matches\n");
# Populate Test array for 2nd test
populate();
$matches = '0';
for ($i=0;$i<$count;$i++) {
$temp = int(rand(@Set));
$guess = $Set[$temp];
$matches += opendoor();
}
print("Trial 2 successes = $matches\n");
sub populate {
for ($i=0;$i<$count;$i++) {
$temp = int(rand($#Set+1));
$Test[$i] = $Set[$temp];
# print("$Test[$i]");
}
}
sub opendoor {
# Cases where guesser chose right on the first try means he will lose
# if switches doors.
if ($guess == $Test[$i]) {return 0;} else {return 1;}
}
Hopefully it doesn't look too horrible... the forums never keep the formatting...
Behind a door there is a prize.
You select a door.
The host opens a door that does not have the prize behind it and asks if you would like to change your choice to the other door.
Should you switch? Does it make a difference?
#!/usr/bin/perl -w
srand($$|time);
$matches = '0';
@Set = qw(1 2 3);
$count = '1000000';
populate();
for ($i=0;$i<$count;$i++) {
$temp = int(rand($#Set+1));
$guess = $Set[$temp];
if ($Test[$i] == $guess) {
$matches++;
}
}
print("Trial 1 successes = $matches\n");
# Populate Test array for 2nd test
populate();
$matches = '0';
for ($i=0;$i<$count;$i++) {
$temp = int(rand(@Set));
$guess = $Set[$temp];
$matches += opendoor();
}
print("Trial 2 successes = $matches\n");
sub populate {
for ($i=0;$i<$count;$i++) {
$temp = int(rand($#Set+1));
$Test[$i] = $Set[$temp];
# print("$Test[$i]");
}
}
sub opendoor {
# Cases where guesser chose right on the first try means he will lose
# if switches doors.
if ($guess == $Test[$i]) {return 0;} else {return 1;}
}
Hopefully it doesn't look too horrible... the forums never keep the formatting...