You're now viewing all of my posts relating to Math. Enjoy!

Computing Royal Flush Probabilities in Ruby

In a surprising turn of events the other day, I actually willingly performed some math. Andy mentioned the odds (different from probability!) of getting a Royal Flush in a game of poker... well intrigued I decided to go about modeling it in Ruby. Maybe I'll try Haskell later...

cards=52
hand=5
winning_combinations=4.00

def factorial(n)
  (n==1) ? n : n*factorial(n-1)
end

combinations=factorial(cards)/( factorial(hand) \
             * factorial(cards - hand) )
p=(winning_combinations/combinations.to_f)
o=p/(1-p)

printf("Combinations: %d\n", combinations)
printf("Winning Combo: %d\n", winning_combinations)
printf("Probability: %0.07f\n", p)
printf("Odds: %0.07f OR %0.07f:%0.07f\n", o, p, (1-p) )
printf("Adjusted Odds: %d:%d\n", 1, \
      (combinations-winning_combinations)/winning_combinations )
Combinations: 2598960
Winning Combo: 4
Probability: 0.0000015
Odds: 0.0000015 OR 0.0000015:0.9999985
Adjusted Odds: 1:649739

Dr. Ernst would be proud. :)