Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save NaelsonDouglas/4abef2357bd5be78e29a464391e7cec6 to your computer and use it in GitHub Desktop.

Select an option

Save NaelsonDouglas/4abef2357bd5be78e29a464391e7cec6 to your computer and use it in GitHub Desktop.
simulations = 100000
GOAT = 0
CAR = 1
REMOVED = -1
#========================Aux functions======================#
function getremoved_door(usr,doorset)
removed_door = -1
for i in keys(doorset)
if (i!=usr && doorset[i]!= CAR)
removed_door = i
end
end
return removed_door
end
function getavailable_door(usr,removed)
available = -1
for i=1:3
if (i!=usr && i!= removed)
available = i
end
end
return available
end
function changedoor(current_door::Int, available_door::Int, change::Bool)
if change
return available_door
else
return current_door
end
end
function simulate(change::Bool)
doors = Dict(1=>GOAT,2=>GOAT,3=>GOAT)
doors[rand(1:3)] = CAR
user_door = rand(1:3)
removed_door = getremoved_door(user_door,doors)
delete!(doors,removed_door)
available_door = getavailable_door(user_door,removed_door)
user_door = changedoor(user_door,available_door,change)
if(doors[user_door] == CAR)
return 1
else
return 0
end
end
#============================================================#
changer_cookies = 0
not_changer_cookies = 0
for i=1:simulations
changer = simulate(true)
not_changer = simulate(false)
if changer == 1
changer_cookies +=1
end
if not_changer == 1
not_changer_cookies +=1
end
if (mod(i,5000) == 0)
info("$i simulations executed")
info("The changer agent scored $changer_cookies times (",round(100*changer_cookies/i,2),"%)")
info("The not changer agent scored $not_changer_cookies times (",round(100*not_changer_cookies/i,2),"%)")
println()
end
end
#===================If the player never changes the door=================#
#=
not_changingwins = 0
for i=1:simulations
not_changingwins += simulate(false)
end
info("$simulations simulations executed by NEVER changing the door\n$not_changingwins games out of $simulations won")
println("Expected optimial result: ",round((1/3) * 100,2),"% ")
println("Matched result: ",round((not_changingwins/simulations)*100,2),"% \n")
#===================If the player always changes the door=================
changingwins = 0
for i=1:simulations
changingwins += simulate(true)
end
info("$simulations simulations executed by ALWAYS changing the door\n$changingwins games out of $simulations won")
println("Expected optimial result: ",round((2/3) * 100,2),"% ")
println("Matched result: ",round((changingwins/simulations)*100,2),"% \n")
=#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment