Monty Hall problem: Difference between revisions
Content added Content deleted
imported>Myrmidon (→{{header|Chapel}}: re-implemented using data parallelism which is simpler and about 10× faster) |
imported>Myrmidon (add standard ml implementation) |
||
Line 4,524: | Line 4,524: | ||
new_line; |
new_line; |
||
end monty;</syntaxhighlight> |
end monty;</syntaxhighlight> |
||
=={{header|Standard ML}}== |
|||
Works with SML/NJ or with MLton using the SML/NJ Util library. |
|||
<syntaxhighlight lang="standard ml"> |
|||
val pidint = Word64.toInt(Posix.Process.pidToWord(Posix.ProcEnv.getpid())); |
|||
val rand = Random.rand(LargeInt.toInt(Time.toSeconds(Time.now())), pidint); |
|||
fun stick_win 0 wins = wins |
|||
| stick_win trial wins = |
|||
let |
|||
val winner_door = (Random.randNat rand) mod 3; |
|||
val chosen_door = (Random.randNat rand) mod 3; |
|||
in |
|||
if winner_door = chosen_door then |
|||
stick_win (trial-1) (wins+1) |
|||
else |
|||
stick_win (trial-1) wins |
|||
end |
|||
val trials = 1000000; |
|||
val sticks = stick_win trials 0; |
|||
val stick_winrate = 100.0 * Real.fromInt(sticks) / Real.fromInt(trials); |
|||
(* if you lost by sticking you would have won by swapping *) |
|||
val swap_winrate = 100.0 - stick_winrate; |
|||
(print ("sticking = " ^ Real.toString(stick_winrate) ^ "% win rate\n"); |
|||
print ("swapping = " ^ Real.toString(swap_winrate) ^ "% win rate\n")); |
|||
</syntaxhighlight> |
|||
'''Output''' |
|||
<pre> |
|||
sticking = 33.3449% win rate |
|||
swapping = 66.6551% win rate |
|||
</pre> |
|||
=={{header|Stata}}== |
=={{header|Stata}}== |