Created
July 23, 2014 02:07
-
-
Save shishirmk/38c69e651e8448579c64 to your computer and use it in GitHub Desktop.
# Just simulating and checking dice throw probabilities and prediction based on least occuring number in history
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
# Just simulating and checking probabilities | |
# Hence proved i was wrong | |
def dice_throw | |
random_gen = Random.new | |
return random_gen.rand(6) + 1 | |
end | |
def random_predict | |
random_gen = Random.new | |
return random_gen.rand(6) + 1 | |
end | |
class DiceThrowPredictor | |
def initialize | |
@throw_history_sequence = [] | |
@throw_counts = {1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0} | |
end | |
def record_throw(num) | |
@throw_history_sequence << num | |
@throw_counts[num] += 1 | |
end | |
def predict | |
lowest_count = Float::INFINITY | |
lowest_count_num = 0 | |
for num, count in @throw_counts | |
if count < lowest_count | |
lowest_count = count | |
lowest_count_num = num | |
end | |
end | |
return lowest_count_num | |
end | |
def throw_counts | |
@throw_counts | |
end | |
end | |
predictor = DiceThrowPredictor.new | |
predictor_score = 0 | |
random_score = 0 | |
total_throws = 0 | |
60000.times do |i| | |
predicted_result = predictor.predict() | |
throw_result = dice_throw() | |
predictor_score += 1 if throw_result == predicted_result | |
random_score += 1 if throw_result == random_predict() | |
total_throws += 1 | |
predictor.record_throw throw_result | |
end | |
puts "Predictor Performance: #{predictor_score}/#{total_throws}" | |
puts "Predictor Performance: #{random_score}/#{total_throws}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment