Jump to content

Monty Hall problem: Difference between revisions

m
whitespace and lang tags
No edit summary
m (whitespace and lang tags)
Line 9:
 
=={{header|ActionScript}}==
<lang actionscript>package {
package {
import flash.display.Sprite;
 
Line 43 ⟶ 42:
}
}
}</lang>
}
</lang>
Output:
<pre>Switching wins 18788 times. (62.626666666666665%)
<pre>
SwitchingStaying wins 1878811212 times. (6237.62666666666666537333333333333%)</pre>
Staying wins 11212 times. (37.37333333333333%)
</pre>
 
=={{header|Ada}}==
<lang ada>-- Monty Hall Game
-- Monty Hall Game
 
with Ada.Text_Io; use Ada.Text_Io;
Line 128 ⟶ 123:
Put_Line("%");
 
end Monty_Stats;</lang>
</lang>
Results
Switch<pre>Stay : count 6569534308 = 6534.6931%
<pre>
StaySwitch : count 3430865695 = 3465.3169%</pre>
Switch : count 65695 = 65.69%
</pre>
 
=={{header|ALGOL 68}}==
Line 202 ⟶ 194:
New random choice: 50.17%
</pre>
 
=={{header|AWK}}==
<pre>
Line 240 ⟶ 233:
}
return rand() <0.5 ? chosen : alternative
 
}
 
function simulate(algo){
prizecount = 0
Line 251 ⟶ 244:
printf " Algorithm %7s: prize count = %i, = %6.2f%%\n", \
algo, prizecount,prizecount*100/iterations
 
}
 
Line 302 ⟶ 294:
<pre>Switching wins 21805 times.
Staying wins 10963 times.</pre>
 
 
=={{header|C}}==
Line 368 ⟶ 359:
printf("Changing: %.2f\n", PERCENT(changewinning) );
printf("New random choice: %.2f\n", PERCENT(randomwinning) );
}</lang>
}
</lang>
 
Output of one run:
 
<pre>Staying: 33.41
Staying: 33.41
Changing: 66.59
New random choice: 49.67</pre>
</pre>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <iostream>
#include <cstdlib>
#include <ctime>
Line 437 ⟶ 424:
int wins_change = check(games, true);
std::cout << "staying: " << 100.0*wins_stay/games << "%, changing: " << 100.0*wins_change/games << "%\n";
}</lang>
}
</lang>
Sample output:
staying: 33.73%, changing: 66.9%
 
=={{header|D}}==
<lang d>import std.stdio, std.random;
import std.stdio, std.random;
 
void main() {
Line 476 ⟶ 461:
 
writefln("Switching wins: %d Staying wins: %d", switchWins, stayWins);
}</lang>
}
</lang>
 
Output:
Line 485 ⟶ 469:
 
=={{header|Forth}}==
<lang forth> include random.fs
variable stay-wins
Line 501 ⟶ 485:
cr switch-wins @ . [char] / emit . ." switching wins" ;
1000 trials</lang>
 
or in iForth:
 
<lang forth> 0 value stay-wins
 
0 value stay-wins
0 value switch-wins
 
Line 519 ⟶ 502:
dup 0 ?DO trial LOOP
CR stay-wins DEC. ." / " dup DEC. ." staying wins,"
CR switch-wins DEC. ." / " DEC. ." switching wins." ;</lang>
 
With output:
 
<pre> FORTH> 100000000 trials
33336877 / 100000000 staying wins,
66663123 / 100000000 switching wins. ok</pre>
 
=={{header|Fortran}}==
Line 648 ⟶ 631:
=={{header|J}}==
 
<lang j> NB. Monty Hall Simulation (a tacit version)
<lang j>
NB. Monty Hall Simulation (a tacit version)
'SIZE CAR STAY MONTY SWITCH DOORS ALL'=. i.7 NB. Setting mnemonics for boxes
Line 676 ⟶ 658:
+--------------+----------------+
|Stay: 0.333143|Switch: 0.666857|
+--------------+----------------+</lang>
</lang>
 
=={{header|Java}}==
Line 772 ⟶ 753:
msg "switch" switch;
msg "stay" stay</lang>
 
 
=={{header|Perl}}==
Line 821 ⟶ 801:
 
=={{header|Python}}==
<lang python>'''
'''
I could understand the explanation of the Monty Hall problem
but needed some more evidence
Line 866 ⟶ 845:
print [monty_hall(randrange(3), switch=True)
for x in range(iterations)].count(True),
print "out of", iterations, "times.\n"</lang>
</lang>
Sample output:
<pre>Monty Hall problem simulation:
Line 877 ⟶ 855:
=={{header|R}}==
 
<lang r> # Since R is a vector based language that penalizes for loops, we will avoid
# for-loops, instead using "apply" statement variants (like "map" in other
# functional languages).
Line 938 ⟶ 916:
change <- runif(N) >= .5
random_switch[change] <- other_door[change]
summary(random_switch == true_answers)</lang>
 
 
<pre>Results:
Results:
 
> ## if always switch
Line 969 ⟶ 946:
> summary(random_switch == true_answers)
Mode FALSE TRUE
logical 4986 5014 </pre>
 
</pre>
 
=={{header|Scheme}}==
<lang scheme>(define (random-from-list list) (list-ref list (random (length list))))
<lang scheme>
(define (random-from-list list) (list-ref list (random (length list))))
(define (random-permutation list)
(if (null? list)
Line 1,041 ⟶ 1,015:
;; > (compare-strategies 1000000)
;; (stay-strategy won with probability 33.3638 %
;; and switch-strategy won with probability 51.8763 %)</lang>
</lang>
 
=={{header|Tcl}}==
Line 1,135 ⟶ 1,108:
 
Vedit macro language does not have random number generator, so one is implemented in subroutine RANDOM (the algorithm was taken from ANSI C library).
</lang vedit>
<pre>
#90 = Time_Tick // seed for random number generator
#91 = 3 // random numbers in range 0 to 2
Line 1,169 ⟶ 1,142:
#90 = (48271 * (#90 % #92) - #93 * (#90 / #92)) & 0x7fffffff
return ((#90 & 0xffff) * #91 / 0x10000)
</prelang>
 
Sample output:
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.