Created
May 23, 2013 18:46
-
-
Save jasl/5638455 to your computer and use it in GitHub Desktop.
three gates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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