Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Last active May 6, 2022 09:27
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 terasakisatoshi/e8ad0eda3000b1559bf9c3549ee615ab to your computer and use it in GitHub Desktop.
Save terasakisatoshi/e8ad0eda3000b1559bf9c3549ee615ab to your computer and use it in GitHub Desktop.
ヤギは引きたくないんじゃ
using Random
using StaticArrays
function selectrest(i::T, j::T) where T <: Integer
(i, j) == (T(1), T(2)) && return T(3)
(i, j) == (T(2), T(1)) && return T(3)
(i, j) == (T(2), T(3)) && return T(1)
(i, j) == (T(3), T(2)) && return T(1)
(i, j) == (T(1), T(3)) && return T(2)
(i, j) == (T(3), T(1)) && return T(2)
return zero(T)
end
function simulate(ntrial)
N = 3
doors = SVector{N, UInt8}(1:N)
result = fill(false, N)
cnt = 0
not = Dict{UInt8, SVector{(N-1), UInt8}}()
for n in 1:N
not[n] = SVector{N-1, UInt8}([i for i in 1:N if i != n])
end
for _ in 1:ntrial
# the chairperson sets prize door
prize_door = rand(doors)
result[prize_door] = true
# you choose a door
guest_door = rand(doors)
lose_door = ifelse(guest_door == prize_door,
rand(not[prize_door]),
selectrest(guest_door, prize_door)
)
new_door = selectrest(lose_door, guest_door)
cnt += ifelse(result[new_door], 1, 0)
# reset state
fill!(result, false)
end
"$(100cnt/ntrial) %"
end
ntrial = 100000000
@time simulate(ntrial)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment