Created
November 2, 2017 18:00
-
-
Save adi-li/1601e58ef10d6dd562ba727b80433758 to your computer and use it in GitHub Desktop.
Monty Hall Problem - Weird Probability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//: Playground - noun: a place where people can play | |
import UIKit | |
func random(lessThan aNumber: Int) -> Int { | |
return Int(arc4random_uniform(UInt32(aNumber))) | |
} | |
var firstWinCount = 0 | |
var audienceWinCount = 0 | |
for _ in 0..<1000 { | |
let boxesCount = 3 | |
let priceBox = random(lessThan: boxesCount) | |
let firstPicked = random(lessThan: boxesCount) | |
let lastOpenedBox = Array(0..<boxesCount).first { | |
$0 != priceBox && $0 != firstPicked | |
}! | |
// Left boxes always be 2, has or no price | |
let leftBoxes: [Int] | |
if firstPicked == priceBox { | |
firstWinCount += 1 | |
leftBoxes = [priceBox, lastOpenedBox] | |
} else { | |
leftBoxes = [priceBox, firstPicked] | |
} | |
// Audience start picked | |
let audiencePicked = leftBoxes[random(lessThan: leftBoxes.count)] | |
if audiencePicked == priceBox { | |
audienceWinCount += 1 | |
} | |
} | |
//😱😱😱 | |
print(firstWinCount) | |
print(audienceWinCount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment