Last active
May 3, 2019 08:14
-
-
Save ylxdzsw/bdb29cd0b35b8e1d450064667d8efe2d to your computer and use it in GitHub Desktop.
关于条件概率的沙雕问题
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
const box1 = ('红', '红') | |
const box2 = ('红', '蓝') | |
const box3 = ('蓝', '蓝') | |
另一个也是红球的次数 = 0 | |
总试验次数 = 0 | |
while 总试验次数 < 100_000 | |
# 随机抽取一个盒子 | |
box = rand([box1, box2, box3]) | |
# 随机抽取一个球 | |
ball = rand(box) | |
# 是红球 | |
if ball == '红' | |
global 总试验次数 += 1 | |
# 另一个也是, 等价于抽到的就是box1 | |
if box == box1 | |
global 另一个也是红球的次数 += 1 | |
end | |
end | |
end | |
println(另一个也是红球的次数 / 总试验次数) # 2/3 |
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
const box1 = ('红', '红') | |
const box2 = ('红', '蓝') | |
const box3 = ('蓝', '蓝') | |
另一个也是红球的次数 = 0 | |
总试验次数 = 0 | |
while 总试验次数 < 100_000 | |
# 随机抽取一个盒子 | |
box = rand([box1, box2, box3]) | |
# 其中有一个是红球 | |
if '红' ∈ box | |
global 总试验次数 += 1 | |
# 另一个也是, 等价于抽到的就是box1 | |
if box == box1 | |
global 另一个也是红球的次数 += 1 | |
end | |
end | |
end | |
println(另一个也是红球的次数 / 总试验次数) # 1/2 |
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 Base.- | |
-(x::NTuple{N, Char}, y::Char) where N = deleteat!([x...], findfirst(x->x == y, x)) |> Tuple | |
const doors = ('车', '羊', '羊') | |
你选的门里是车的次数 = 0 | |
另一个门里是车的次数 = 0 | |
总试验次数 = 0 | |
while 总试验次数 < 100_000 | |
# 随机抽取一个门 | |
choice = rand(doors) | |
remaining = doors - choice | |
# 主持人打开一个有羊的门 | |
remaining = remaining - '羊' | |
global 你选的门里是车的次数 += choice == '车' | |
global 另一个门里是车的次数 += remaining[1] == '车' | |
global 总试验次数 += 1 | |
end | |
println(你选的门里是车的次数 / 总试验次数) # 1/3 | |
println(另一个门里是车的次数 / 总试验次数) # 2/3 |
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 Base.- | |
-(x::NTuple{N, Char}, y::Char) where N = deleteat!([x...], findfirst(x->x == y, x)) |> Tuple | |
const doors = ('车', '羊', '羊') | |
你选的门里是车的次数 = 0 | |
另一个门里是车的次数 = 0 | |
总试验次数 = 0 | |
while 总试验次数 < 100_000 | |
# 随机抽取一个门 | |
choice = rand(doors) | |
remaining = doors - choice | |
# 主持人让观众随机打开一个门,发现里面是羊 | |
guest_choice = rand(remaining) | |
if guest_choice == '羊' | |
remaining = remaining - guest_choice | |
global 总试验次数 += 1 | |
global 你选的门里是车的次数 += choice == '车' | |
global 另一个门里是车的次数 += remaining[1] == '车' | |
end | |
end | |
println(你选的门里是车的次数 / 总试验次数) # 1/2 | |
println(另一个门里是车的次数 / 总试验次数) # 1/2 |
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
另一个也是女孩的次数 = 0 | |
总试验次数 = 0 | |
while 总试验次数 < 100_000 | |
一胎 = rand(['男', '女']) | |
二胎 = rand(['男', '女']) | |
# 其中有一个是女孩 | |
if '女' ∈ (一胎, 二胎) | |
global 总试验次数 += 1 | |
# 另一个也是女孩 | |
if 一胎 == 二胎 == '女' | |
global 另一个也是女孩的次数 += 1 | |
end | |
end | |
end | |
println(另一个也是女孩的次数 / 总试验次数) # 1/3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment