Anonymous user
Monty Hall problem: Difference between revisions
m
Fixed lang tags.
Underscore (talk | contribs) (→{{header|Perl 6}}: Changed to call &pick less often.) |
m (Fixed lang tags.) |
||
Line 132:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang
PROC brand = (INT n)INT: 1 + ENTIER (n * random);
Line 194:
=={{header|AWK}}==
<lang awk>#!/bin/gawk -f
# Monty Hall problem
Line 251 ⟶ 250:
simulate(RAND)
}</lang>
Sample output:
<lang awk>bash$ ./monty_hall.awk
Monty Hall problem simulation:
Line 263 ⟶ 260:
Algorithm switch: prize count = 6655, = 66.55%
Algorithm random: prize count = 4991, = 49.91%
bash$</
=={{header|BASIC}}==
Line 500 ⟶ 497:
=={{header|Forth}}==
<lang forth>
or in iForth:
<lang forth>
With output:
Line 543 ⟶ 540:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran>
Sample Output
Chance of winning by not switching is 32.82%
Line 594 ⟶ 591:
=={{header|Haskell}}==
<
trials :: Int
Line 627 ⟶ 624:
percent n ++ "% of the time."
percent n = show $ round $
100 * (fromIntegral n) / (fromIntegral trials)</
{{libheader|mtl}}
With a <tt>State</tt> monad, we can avoid having to explicitly pass around the <tt>StdGen</tt> so often. <tt>play</tt> and <tt>cars</tt> can be rewritten as follows:
<
play :: Bool -> State StdGen Door
Line 652 ⟶ 649:
cars n switch g = (numcars, new_g)
where numcars = length $ filter (== Car) prize_list
(prize_list, new_g) = runState (replicateM n (play switch)) g</
Sample output (for either implementation):
<lang haskell>The switch strategy succeeds 67% of the time.
The
=={{header|J}}==
<lang j>NB. Monty Hall Simulation (a tacit version)
'SIZE CAR STAY MONTY SWITCH DOORS ALL'=. i.7 NB. Setting mnemonics for boxes
f=. &({::)
o=. @:
pick=. (? @ #) { ] NB. Picking randomly an element from a vector
freq=. +/ % #
PickDoors=. (SIZE f ?@$ DOORS f) NB. Picking doors randomly
boxes=. < , a: $~ 6: NB. Appending 6 (empty) boxes to the input
doors=. < o 3: DOORS} ] NB. 3 doors
car=. < o PickDoors CAR} ] NB. Randomizing car door positions
stay=. < o PickDoors STAY} ] NB. Randomizing stay door selections
all=. < o (i. o (DOORS f)) ALL} ] NB. All doors set
Monty=. < o ((pick&>) o (ALL f (< @ -.)"1 (CAR f ,"0 STAY f))) MONTY } ]
NB. Calculating Monty's selections
switch=. < o (, o ((ALL f) -."1 (MONTY f) ,"0 (STAY f))) SWITCH } ]
NB. Calculating switching selections
StayDisplay=. 'Stay: ' , ": o (CAR f freq o = STAY f)
SwitchDisplay=. 'Switch: ' , ": o (CAR f freq o = SWITCH f)
sim=. (StayDisplay ; SwitchDisplay) o switch o Monty o all o stay o car o doors o boxes f.</lang>
Example:
<lang j> sim 1000000
+--------------+----------------+
|Stay: 0.333143|Switch: 0.666857|
+--------------+----------------+</lang>
=={{header|Java}}==
Line 727 ⟶ 718:
=={{header|MAXScript}}==
<lang maxscript>fn montyHall choice switch =
(
doors = #(false, false, false)
Line 752 ⟶ 742:
iterations = 10000
format ("Stay strategy:%\%\n") (iterate iterations false)
format ("Switch strategy:%\%\n") (iterate iterations true)</lang>
Output:
<
Switch strategy:66.84%</
=={{header|OCaml}}==
Line 959 ⟶ 948:
=={{header|R}}==
<lang r>
set.seed(19771025) # set the seed to set the same results as this code
N <- 10000 # trials
true_answers <- sample(1:3, N, replace=TRUE)
# We can assme that the contestant always choose door 1 without any loss of
# generality, by equivalence. That is, we can always relabel the doors
# to make the user-chosen door into door 1.
# Thus, the host opens door '2' unless door 2 has the prize, in which case
# the host opens door 3.
host_opens <- 2 + (true_answers == 2)
other_door <- 2 + (true_answers != 2)
## if always switch
summary( other_door == true_answers )
## if we never switch
## if we randomly switch
random_switch <- other_door
random_switch[runif(N) >= .5] <- 1
summary(random_switch == true_answers)
## To go with the exact parameters of the Rosetta challenge, complicating matters....
## Note that the player may initially choose any of the three doors (not just Door 1),
## that the host opens a different door revealing a goat (not necessarily Door 3), and
## that he gives the player a second choice between the two remaining unopened doors.
N <- 10000 #trials
true_answers <- sample(1:3, N, replace=TRUE)
user_choice <- sample(1:3, N, replace=TRUE)
## the host_choice is more complicated
host_chooser <- function(user_prize) {
# this could be cleaner
bad_choices <- unique(user_prize)
# in R, the x[-vector] form implies, choose the indices in x not in vector
choices <- c(1:3)[-bad_choices]
# if the first arg to sample is an int, it treats it as the number of choices
if (length(choices) == 1) { return(choices)}
else { return(sample(choices,1))}
}
host_choice <- apply( X=cbind(true_answers,user_choice), FUN=host_chooser,MARGIN=1)
not_door <- function(x){ return( (1:3)[-x]) } # we could also define this
# directly at the FUN argument following
other_door <- apply( X = cbind(user_choice,host_choice), FUN=not_door, MARGIN=1)
## if always switch
summary( other_door == true_answers )
## if we never switch
summary( true_answers == user_choice)
## if we randomly switch
random_switch <- user_choice
change <- runif(N) >= .5
random_switch[change] <- other_door[change]
summary(random_switch == true_answers)</lang>
Line 1,250 ⟶ 1,239:
for the switching strategy.
<lang Ursala>#import std
#import nat
#import flo
Line 1,267 ⟶ 1,255:
#show+
main = ~&plrTS/<'stay: ','switch: '> format* <staying_wins,switching_wins></lang>
Output will vary slightly for each run due to randomness.
<pre>
Line 1,279 ⟶ 1,266:
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>#90 = Time_Tick // seed for random number generator
#91 = 3 // random numbers in range 0 to 2
#1 = 0 // wins for "always stay" strategy
Line 1,313 ⟶ 1,299:
#93 = 0x7fffffff % 48271
#90 = (48271 * (#90 % #92) - #93 * (#90 / #92)) & 0x7fffffff
return ((#90 & 0xffff) * #91 / 0x10000)</lang>
Sample output:
|