Probability

This weekend I wanted to show my kids a little lesson in probability. First we did a small test of the Monty Hall problem. As expected, they didn't want to switch doors. After 20 tests, it showed that it was slightly better to switch (5 times she switched doors and won on 3 of those, for the 15 times she didn't switch resulted in 4 wins), so the data did show the expected results, but it took a lot of work.

With a small amount of explanation, I opened a workspace in Pharo and demonstrated how to test the probability of guessing the right door:

| prize pick count |
count := 0.
1000000 timesRepeat: [ prize := (1 to: 3) atRandom.
	pick := (1 to: 3) atRandom.
	pick = prize ifTrue: [ count := count + 1 ]].
(count / 1000000) asFloat

Running the test 20, then 100, then a million showed how the probability approached 1/3. Next added a little code to show what happened if the contestant could switch doors:

| prize pick count notThisDoor |
count := 0.
1000000 timesRepeat: [ prize := (1 to: 3) atRandom.
	pick := (1 to: 3) atRandom.
	notThisDoor := ((1 to: 3) reject: [ :number | number = pick or: [ number = prize ] ]) atRandom.
	pick := ((1 to: 3) reject: [ :number | number = pick or: [ number = notThisDoor ] ]) atRandom.
	pick = prize ifTrue: [ count := count + 1 ]].
(count / 1000000) asFloat

The second assignment to pick could use first instead of atRandom, however that would mean explaining another message.

Posted by John Borden at 20 February 2017, 1:06 pm link