Skip to content

Instantly share code, notes, and snippets.

@noniq
Last active September 20, 2015 23:32
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 noniq/7ee59324e08937b77e2c to your computer and use it in GitHub Desktop.
Save noniq/7ee59324e08937b77e2c to your computer and use it in GitHub Desktop.
STAY, 4989 relevant rounds (= 49.9%)
2531 successes (= 50.7%)
CHANGE, 4989 relevant rounds (= 49.9%)
2493 successes (= 50.0%)
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