Last active
September 20, 2015 23:32
-
-
Save noniq/7ee59324e08937b77e2c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
STAY, 4989 relevant rounds (= 49.9%) | |
2531 successes (= 50.7%) | |
CHANGE, 4989 relevant rounds (= 49.9%) | |
2493 successes (= 50.0%) |
This file contains hidden or 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
num_rounds = 10000 | |
answers = %W(A B C D) | |
strategies = %W(STAY CHANGE) | |
# Helfermethode und Alias für bessere Lesbarkeit | |
class Array | |
def except(element) | |
self - [element] | |
end | |
alias choose_randomly sample | |
end | |
strategies.each do |strategy| | |
num_successes = 0 | |
num_skipped_rounds = 0 | |
num_rounds.times do | |
correct_answer = answers.choose_randomly | |
initial_choice = answers.choose_randomly | |
joker_result = [correct_answer, answers.except(correct_answer).choose_randomly].sort | |
if joker_result.include?(initial_choice) | |
final_choice = if strategy == "CHANGE" | |
joker_result.except(initial_choice).first | |
else | |
initial_choice | |
end | |
success = final_choice == correct_answer | |
num_successes += 1 if success | |
# puts "#{correct_answer} : #{initial_choice} - #{joker_result.join(' ')} - #{final_choice} : #{success}" | |
else | |
# puts "#{correct_answer} : #{initial_choice} - #{joker_result.join(' ')} - SKIPPED" | |
num_skipped_rounds += 1 | |
end | |
end | |
num_relevant_rounds = num_rounds - num_skipped_rounds | |
puts "#{strategy}, #{num_relevant_rounds} relevant rounds (= %.1f%%)" % (num_relevant_rounds * 100.0 / num_rounds) | |
puts "#{num_successes} successes (= %.1f%%)" % (num_successes * 100.0 / num_relevant_rounds) | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment