Skip to content

Instantly share code, notes, and snippets.

@adi-li
Created November 2, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adi-li/1601e58ef10d6dd562ba727b80433758 to your computer and use it in GitHub Desktop.
Save adi-li/1601e58ef10d6dd562ba727b80433758 to your computer and use it in GitHub Desktop.
Monty Hall Problem - Weird Probability
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