Skip to content

Instantly share code, notes, and snippets.

@jasl
Created May 23, 2013 18:46
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 jasl/5638455 to your computer and use it in GitHub Desktop.
Save jasl/5638455 to your computer and use it in GitHub Desktop.
three gates
require 'securerandom'
def seed
SecureRandom.uuid.gsub(/[-a-z]/,"").to_i
end
total = 10000
rights = 0
(1..total).each do
# gen a random doors
doors = [false, false, false]
doors[Random.new(seed).rand(3)] = true
# choose a door
rights += 1 if doors[Random.new(seed).rand(3)]
end
puts "insist: rights: #{rights}, probability: #{rights.to_f / total}."
rights = 0
(1..total).each do
# gen a random doors
doors = [false, false, false]
doors[Random.new(seed).rand(3)] = true
indexes = [0, 1, 2]
# choose a door
c = Random.new(seed).rand(3)
# remove a wrong door
indexes.delete c
until indexes.length == 1
i = Random.new(seed).rand(3)
indexes.delete i unless doors[i]
end
rights += 1 if doors[indexes[0]]
end
puts "change: rights #{rights}, probability: #{rights.to_f / total}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment