RosettaCodeData/Task/Monty-Hall-problem/Scilab/monty-hall-problem.scilab
2023-07-01 13:44:08 -04:00

38 lines
1.4 KiB
Text

// How it works:
// MontyHall() is a function with argument switch:
// it will be called 100000 times with switch=%T,
// and another 100000 times with switch=%F
function win=MontyHall(switch) //If switch==%T the player will switch
doors=zeros(1,3) //All goats
car=grand(1,1,'uin',1,3)
a(car)=1 //Place a car somewher
pick=grand(1,1,'uin',1,3) //The player picks...
if pick==car then //If the player picks right...
if switch==%T then //...and switches he will be wrong
win=%F
else //...but if he doesn't, he will be right
win=%T
end
else //If the player picks a goat...
if switch==%T then //...and switches: the other door with the goat shall be
win=%T // opened: the player will switch to the car and win
else //...but if he doesn't, he will remain by his goat
win=%F
end
end
endfunction
wins_switch=0
wins_stay=0
games=100000
for i=1:games
if MontyHall(%T)==%T then
wins_switch=wins_switch+1
end
if MontyHall(%F)==%T then
wins_stay=wins_stay+1
end
end
disp("Switching, one wins"+ascii(10)+string(wins_switch)+" games out of "+string(games))
disp("Staying, one wins"+ascii(10)+string(wins_stay)+" games out of "+string(games))