emdrone (emdrone) wrote,
emdrone
emdrone

Задача Монти Холла

http://en.wikipedia.org/wiki/Monty_Hall_problem

#!/usr/bin/perl -w

# A contestant is shown $numdoors closed doors; behind one is a car, and behind
# each of the others is a goat. The contestant chooses one door and will be
# allowed to keep what is behind it. Before the door is opened, however, the
# host opens all other doors except one (i.e. $numdoors-2 ) and shows that
# there is a goat behind each of them.
# Should the contestant stick with the original choice or change to the
# remaining door; or does it make no difference?

#-- the program models outcomes of the tests -------
# USAGE: ./paradox.pl numtests same|different numdoors
# where 'numtests' and 'numdoors' are numbers.

#-------primitive argument processing---
if(@ARGV != 3 ){print("\n", 'USAGE: "./paradox.pl numtests same numdoors"
or "./paradox.pl numtests different numdoors"', "\n\n");exit}
elsif($ARGV[1] eq "same"){$selfinal=0}
elsif($ARGV[1] eq "different"){$selfinal=1}
else{print('USAGE: "./paradox.pl numtests same numdoors"
or "./paradox.pl numtests different numdoors"',"\n");exit}

#----counters
$countwins=0;
$countlosses=0;
# ----- the car is behind door number $car --------
$numdoors=$ARGV[2];

#------looping over tests--
for ($i=$ARGV[0]; $i>0; $i--) {

$car=int(rand($numdoors))+1;
#----- I randomly select door number $sel1
$sel1=int(rand($numdoors))+1;

if (($car)==$sel1){
if($car==$numdoors){$door[0]=$car;$door[1]=$car-1}
else{$door[0]=$car;$door[1]=$car+1}
} #-------- I got the car (without knowing it), and one arbitrary sheep has to go;
#------- it means my guess is in $door[0], while $door[1] is arbitrary
else {$door[0]=$sel1;$door[1]=$car} #---- i.e. my sel. was sheep, my guess is in $door[0]

#----- So, my guess is in $door[0]. Now counting whether the car was won or not
#--- "same" means we cmp $door[0] with $car; "different" means it's $door[1]:
if ($door[$selfinal] == $car){
#----comment or uncomment next line to hide or see output
print (' WON!!!', "\n");
$countwins++}
else{
#------comment or uncomment next line to hide or see output
print (' lost..', "\n");
$countlosses++}

} #-----end of the tests loop

print("\n", 'Number of wins...', $countwins, ' ...Number of losses...', $countlosses, "\n\n")
Subscribe

  • Первое впечатление

    Как правило первые и эмоциональные впечтления бывают неверны, но всё же... ... обращение к нации Путина сегодня, 21 апреля 2021, вызвало у меня…

  • Современнае Абразаванийе

    .. или что такое cancel culture В связи с нынешними событиями по отключениям президентов - и первой волной ukaze Байдена, а также недавними…

  • Короткое замечание о главном

    В ленте увидел ссылку на пост против вчерашних демонстраций детей. Автор выдаёт кучу вопросов, дескать, а понимаете ли вы, что вы это делаете во…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 5 comments