Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
51
Task/Monty-Hall-problem/Chapel/monty-hall-problem.chapel
Normal file
51
Task/Monty-Hall-problem/Chapel/monty-hall-problem.chapel
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use Random;
|
||||
|
||||
param doors: int = 3;
|
||||
config const games: int = 1000;
|
||||
|
||||
config const maxTasks = 32;
|
||||
var numTasks = 1;
|
||||
while( games / numTasks > 1000000 && numTasks < maxTasks ) do numTasks += 1;
|
||||
const tasks = 1..#numTasks;
|
||||
const games_per_task = games / numTasks ;
|
||||
const remaining_games = games % numTasks ;
|
||||
|
||||
var wins_by_stay: [tasks] int;
|
||||
|
||||
coforall task in tasks {
|
||||
|
||||
var rand = new RandomStream();
|
||||
|
||||
for game in 1..#games_per_task {
|
||||
var player_door = (rand.getNext() * 1000): int % doors ;
|
||||
var winning_door = (rand.getNext() * 1000): int % doors ;
|
||||
if player_door == winning_door then
|
||||
wins_by_stay[ task ] += 1;
|
||||
}
|
||||
|
||||
if task == tasks.last then {
|
||||
for game in 1..#remaining_games {
|
||||
var player_door = (rand.getNext() * 1000): int % doors ;
|
||||
var winning_door = (rand.getNext() * 1000): int % doors ;
|
||||
if player_door == winning_door then
|
||||
wins_by_stay[ task ] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var total_by_stay = + reduce wins_by_stay;
|
||||
|
||||
var total_by_switch = games - total_by_stay;
|
||||
var percent_by_stay = ((total_by_stay: real) / games) * 100;
|
||||
var percent_by_switch = ((total_by_switch: real) / games) * 100;
|
||||
|
||||
writeln( "Wins by staying: ", total_by_stay, " or ", percent_by_stay, "%" );
|
||||
writeln( "Wins by switching: ", total_by_switch, " or ", percent_by_switch, "%" );
|
||||
if ( total_by_stay > total_by_switch ){
|
||||
writeln( "Staying is the superior method." );
|
||||
} else if( total_by_stay < total_by_switch ){
|
||||
writeln( "Switching is the superior method." );
|
||||
} else {
|
||||
writeln( "Both methods are equal." );
|
||||
}
|
||||
24
Task/Monty-Hall-problem/Elixir/monty-hall-problem.elixir
Normal file
24
Task/Monty-Hall-problem/Elixir/monty-hall-problem.elixir
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
defmodule MontyHall do
|
||||
def simulate(n) do
|
||||
:random.seed(:os.timestamp)
|
||||
{stay, switch} = simulate(n, 0, 0)
|
||||
:io.format "Staying wins ~w times (~.3f%)~n", [stay, 100 * stay / n]
|
||||
:io.format "Switching wins ~w times (~.3f%)~n", [switch, 100 * switch / n]
|
||||
end
|
||||
|
||||
defp simulate(0, stay, switch), do: {stay, switch}
|
||||
defp simulate(n, stay, switch) do
|
||||
doors = Enum.shuffle([:goat, :goat, :car])
|
||||
guess = :random.uniform(3) - 1
|
||||
[choice] = [0,1,2] -- [guess, shown(doors, guess)]
|
||||
if Enum.at(doors, choice) == :car, do: simulate(n-1, stay, switch+1),
|
||||
else: simulate(n-1, stay+1, switch)
|
||||
end
|
||||
|
||||
defp shown(doors, guess) do
|
||||
[i, j] = Enum.shuffle([0,1,2] -- [guess])
|
||||
if Enum.at(doors, i) == :goat, do: i, else: j
|
||||
end
|
||||
end
|
||||
|
||||
MontyHall.simulate(10000)
|
||||
20
Task/Monty-Hall-problem/Julia/monty-hall-problem-1.julia
Normal file
20
Task/Monty-Hall-problem/Julia/monty-hall-problem-1.julia
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
function play_mh_literal{T<:Integer}(ncur::T=3, ncar::T=1)
|
||||
ncar < ncur || throw(DomainError())
|
||||
curtains = shuffle(collect(1:ncur))
|
||||
cars = curtains[1:ncar]
|
||||
goats = curtains[(ncar+1):end]
|
||||
pick = rand(1:ncur)
|
||||
isstickwin = pick in cars
|
||||
deleteat!(curtains, findin(curtains, pick))
|
||||
if !isstickwin
|
||||
deleteat!(goats, findin(goats, pick))
|
||||
end
|
||||
if length(goats) > 0 # reveal goat
|
||||
deleteat!(curtains, findin(curtains, shuffle(goats)[1]))
|
||||
else # no goats, so reveal car
|
||||
deleteat!(curtains, rand(1:(ncur-1)))
|
||||
end
|
||||
pick = shuffle(curtains)[1]
|
||||
isswitchwin = pick in cars
|
||||
return (isstickwin, isswitchwin)
|
||||
end
|
||||
11
Task/Monty-Hall-problem/Julia/monty-hall-problem-2.julia
Normal file
11
Task/Monty-Hall-problem/Julia/monty-hall-problem-2.julia
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function play_mh_clean{T<:Integer}(ncur::T=3, ncar::T=1)
|
||||
ncar < ncur || throw(DomainError())
|
||||
pick = rand(1:ncur)
|
||||
isstickwin = pick <= ncar
|
||||
pick = rand(1:(ncur-2))
|
||||
if isstickwin # remove initially picked car from consideration
|
||||
pick += 1
|
||||
end
|
||||
isswitchwin = pick <= ncar
|
||||
return (isstickwin, isswitchwin)
|
||||
end
|
||||
44
Task/Monty-Hall-problem/Julia/monty-hall-problem-3.julia
Normal file
44
Task/Monty-Hall-problem/Julia/monty-hall-problem-3.julia
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
function mh_results{T<:Integer}(ncur::T, ncar::T,
|
||||
nruns::T, play_mh::Function)
|
||||
stickwins = 0
|
||||
switchwins = 0
|
||||
for i in 1:nruns
|
||||
(isstickwin, isswitchwin) = play_mh(ncur, ncar)
|
||||
if isstickwin
|
||||
stickwins += 1
|
||||
end
|
||||
if isswitchwin
|
||||
switchwins += 1
|
||||
end
|
||||
end
|
||||
return (stickwins/nruns, switchwins/nruns)
|
||||
end
|
||||
|
||||
function mh_analytic{T<:Integer}(ncur::T, ncar::T)
|
||||
stickodds = ncar/ncur
|
||||
switchodds = (ncar - stickodds)/(ncur-2)
|
||||
return (stickodds, switchodds)
|
||||
end
|
||||
|
||||
function show_odds{T<:Real}(a::T, b::T)
|
||||
@sprintf " %.1f %.1f %.2f" 100.0*a 100*b 1.0*b/a
|
||||
end
|
||||
|
||||
function show_simulation{T<:Integer}(ncur::T, ncar::T, nruns::T)
|
||||
println()
|
||||
print("Simulating a ", ncur, " door, ", ncar, " car ")
|
||||
println("Monty Hall problem with ", nruns, " runs.\n")
|
||||
|
||||
println(" Solution Stick Switch Improvement")
|
||||
|
||||
(a, b) = mh_results(ncur, ncar, nruns, play_mh_literal)
|
||||
println(@sprintf("%10s: ", "Literal"), show_odds(a, b))
|
||||
|
||||
(a, b) = mh_results(ncur, ncar, nruns, play_mh_clean)
|
||||
println(@sprintf("%10s: ", "Clean"), show_odds(a, b))
|
||||
|
||||
(a, b) = mh_analytic(ncur, ncar)
|
||||
println(@sprintf("%10s: ", "Analytic"), show_odds(a, b))
|
||||
println()
|
||||
return nothing
|
||||
end
|
||||
3
Task/Monty-Hall-problem/Julia/monty-hall-problem-4.julia
Normal file
3
Task/Monty-Hall-problem/Julia/monty-hall-problem-4.julia
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
for i in 3:5, j in 1:(i-2)
|
||||
show_simulation(i, j, 10^5)
|
||||
end
|
||||
|
|
@ -5,15 +5,18 @@ sub play (Strategy $strategy, Int :$doors = 3) returns Prize {
|
|||
|
||||
# Call the door with a car behind it door 0. Number the
|
||||
# remaining doors starting from 1.
|
||||
my Prize @doors = Car, Goat xx $doors - 1;
|
||||
my Prize @doors = flat Car, Goat xx $doors - 1;
|
||||
|
||||
# The player chooses a door.
|
||||
my Prize $initial_pick = @doors.splice(@doors.keys.pick,1)[0];
|
||||
|
||||
# Of the n doors remaining, the host chooses n - 1 that have
|
||||
# goats behind them and opens them, removing them from play.
|
||||
@doors.splice($_,1)
|
||||
for pick @doors.elems - 1, grep { @doors[$_] == Goat }, keys @doors;
|
||||
while @doors > 1 {
|
||||
@doors.splice($_,1)
|
||||
when Goat
|
||||
given @doors.keys.pick;
|
||||
}
|
||||
|
||||
# If the player stays, they get their initial pick. Otherwise,
|
||||
# they get whatever's behind the remaining door.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue