Last active
December 13, 2022 19:33
-
-
Save mchail/4ba2f3ee1c33fbd4336b to your computer and use it in GitHub Desktop.
Calculate probability that a hand of cards is dealt shuffled
This file contains 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
class Dealer | |
def initialize | |
@deck = new_deck | |
end | |
def deal(size) | |
if @deck.size < size | |
@deck = new_deck | |
end | |
@deck.pop(size) | |
end | |
def new_deck | |
((1..13).to_a * 4).shuffle | |
end | |
end | |
class Array | |
def in_order? | |
current = 0 | |
each do |card| | |
if card < current | |
return false | |
end | |
current = card | |
end | |
return true | |
end | |
end | |
hand_size = 10 | |
trial_count = 1_000_000 | |
wins = 0 | |
dealer = Dealer.new | |
trial_count.times do | |
hand = dealer.deal(hand_size) | |
if hand.in_order? | |
wins += 1 | |
end | |
end | |
puts "#{wins} shuffled out of #{trial_count} trials" | |
puts "#{(wins * 1.0 / trial_count * 100).round(6)}% success rate" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment