Monty Hall problem: Difference between revisions
Content added Content deleted
(→Tcl: Added implementation) |
No edit summary |
||
Line 645: | Line 645: | ||
The stay strategy succeeds 34% of the time. |
The stay strategy succeeds 34% of the time. |
||
</pre> |
</pre> |
||
=={{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=. &({::) NB. Fetching the contents of a box |
|||
o=. @: NB. Composing verbs (functions) |
|||
pick=. (? @ #) { ] NB. Picking randomly an element from a vector |
|||
freq=. +/ % # |
|||
PickDoors=. (SIZE f ?@$ DOORS f) NB. Picking doors randomly |
|||
SetBoxes=. '. . . . . .'&(] , ;: o [) o < |
|||
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 SetBoxes f. |
|||
sim 1000000 |
|||
+--------------+----------------+ |
|||
|Stay: 0.333143|Switch: 0.666857| |
|||
+--------------+----------------+ |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |