Skip to content

Instantly share code, notes, and snippets.

@sirupsen
Last active September 14, 2024 10:43

Revisions

  1. sirupsen revised this gist Sep 13, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion monty_hall.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    switch_strategy = 0
    no_switch_strategy = 0
    n_simulations = 100_000
    n_simulations = 1_000_000

    n_simulations.times do
    winner_door = rand(3)
    @@ -21,3 +21,5 @@

    puts "Switch strategy wins: #{switch_strategy} (#{(switch_strategy.to_f / n_simulations * 100).round(2)}%}"
    puts "No Switch strategy wins: #{no_switch_strategy} (#{(no_switch_strategy.to_f / n_simulations * 100).round(2)}%)"
    # Switch strategy wins: 666481 (66.65%}
    # No Switch strategy wins: 333519 (33.35%)
  2. sirupsen created this gist Sep 13, 2021.
    23 changes: 23 additions & 0 deletions monty_hall.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    switch_strategy = 0
    no_switch_strategy = 0
    n_simulations = 100_000

    n_simulations.times do
    winner_door = rand(3)
    chosen_door = rand(3)

    if winner_door == chosen_door
    switch_strategy += 0
    no_switch_strategy += 1
    else
    # The host always opens the other non-winning door and not your door, this
    # reveals 'information' about your door.
    revealed_door = [0, 1, 3] - [winner_door] - [chosen_door]
    chosen_door = winner_door
    switch_strategy += 1
    no_switch_strategy += 0
    end
    end

    puts "Switch strategy wins: #{switch_strategy} (#{(switch_strategy.to_f / n_simulations * 100).round(2)}%}"
    puts "No Switch strategy wins: #{no_switch_strategy} (#{(no_switch_strategy.to_f / n_simulations * 100).round(2)}%)"