Monty Hall 模拟仅 44%
Monty Hall simulation only 44%
我刚刚为 Monty Hall 问题创建了一个模拟,但我的结果(即使有 10,000,000 次测试)还是很奇怪。对于策略 1(保持),命中率为 1/3,而策略 2(转换)为 44.44%。代码有错吗?
谢谢大家!
var hits1 = 0
var hits2 = 0
let testsNumber = 1000
for i in 0..<testsNumber {
var doors: [Int] = []
for i in 0..<3 {
doors.append(0) // Append closed door
}
doors[Int(arc4random_uniform(UInt32(doors.count)))] = 1 // Here's the car...
var selection = Int(arc4random_uniform(UInt32(doors.count))) // Select door
if doors[selection] == 1 {
hits1 += 1
}
// Open first closed door
for i in 0..<doors.count {
if doors[i] != 1 {
doors[i] = -1 // Open door
break
}
}
// Switch to next closed door
repeat {
selection = (selection + 1) % doors.count
} while(doors[selection] == -1)
if doors[selection] == 1 {
hits2 += 1
}
}
print("Hits: \(hits1), quote: \((Double) (hits1) / (Double) (testsNumber))")
print("Hits: \(hits2), quote: \((Double) (hits2) / (Double) (testsNumber))")
Monty Hall 问题说 "pick a door; before I open it, I'll show what's behind one of the other doors (one that doesn't have a car) and let you stay with your initial selection or switch to the other closed door"。
但请考虑:
for i in 0 ..< doors.count {
if doors[i] != 1 {
doors[i] = -1 // Open door
break
}
}
这实际上是说 "show the contestant what's behind the first door that doesn't have a car"。
但是,您没有考虑这扇门可能是参赛者已经选择的那扇门的可能性。那就是改变游戏的参数。
你的意思是"open a door that does not have a car and is not the door the contestant picked."
for i in 0 ..< doors.count {
if doors[i] != 1 && i != selection {
doors[i] = -1 // Open door
break
}
}
当你这样做时,你总是改变你的选择(已经显示其他两扇没有车的门之一)的几率上升到 2/3。
我刚刚为 Monty Hall 问题创建了一个模拟,但我的结果(即使有 10,000,000 次测试)还是很奇怪。对于策略 1(保持),命中率为 1/3,而策略 2(转换)为 44.44%。代码有错吗?
谢谢大家!
var hits1 = 0
var hits2 = 0
let testsNumber = 1000
for i in 0..<testsNumber {
var doors: [Int] = []
for i in 0..<3 {
doors.append(0) // Append closed door
}
doors[Int(arc4random_uniform(UInt32(doors.count)))] = 1 // Here's the car...
var selection = Int(arc4random_uniform(UInt32(doors.count))) // Select door
if doors[selection] == 1 {
hits1 += 1
}
// Open first closed door
for i in 0..<doors.count {
if doors[i] != 1 {
doors[i] = -1 // Open door
break
}
}
// Switch to next closed door
repeat {
selection = (selection + 1) % doors.count
} while(doors[selection] == -1)
if doors[selection] == 1 {
hits2 += 1
}
}
print("Hits: \(hits1), quote: \((Double) (hits1) / (Double) (testsNumber))")
print("Hits: \(hits2), quote: \((Double) (hits2) / (Double) (testsNumber))")
Monty Hall 问题说 "pick a door; before I open it, I'll show what's behind one of the other doors (one that doesn't have a car) and let you stay with your initial selection or switch to the other closed door"。
但请考虑:
for i in 0 ..< doors.count {
if doors[i] != 1 {
doors[i] = -1 // Open door
break
}
}
这实际上是说 "show the contestant what's behind the first door that doesn't have a car"。
但是,您没有考虑这扇门可能是参赛者已经选择的那扇门的可能性。那就是改变游戏的参数。
你的意思是"open a door that does not have a car and is not the door the contestant picked."
for i in 0 ..< doors.count {
if doors[i] != 1 && i != selection {
doors[i] = -1 // Open door
break
}
}
当你这样做时,你总是改变你的选择(已经显示其他两扇没有车的门之一)的几率上升到 2/3。