Instantly share code, notes, and snippets.

@noniq /Sample output
Last active Sep 20, 2015

Embed
What would you like to do?
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