Skip to content

Instantly share code, notes, and snippets.

@seven1m
Created September 22, 2010 21:30
Show Gist options
  • Save seven1m/592622 to your computer and use it in GitHub Desktop.
Save seven1m/592622 to your computer and use it in GitHub Desktop.
require 'pp'
class Card
attr_reader :face_up
def initialize(face_up)
@face_up = face_up
end
def flip
@face_up ^= true
end
def inspect
"<Card #{face_up ? 'Up' : 'Down'}>"
end
end
def trick
deck = (
(1..42).map { Card.new(false) } +
(1..10).map { Card.new(true) }
).sort_by { rand }
new_deck = deck.pop(10)
new_deck.each { |c| c.flip }
deck_face_up_count = deck.count { |c| c.face_up }
new_deck_face_up_count = new_deck.count { |c| c.face_up }
[deck_face_up_count, new_deck_face_up_count]
end
counts = {}
100000.times do
c1, c2 = trick
if c1 != c2
puts "Error! #{c1} != #{c2}"
end
counts[c1] ||= 0
counts[c1] += 1
end
counts.sort.each do |num, count|
puts "#{num} => #{count}"
end
@sophiebits
Copy link

Here is the theoretical distribution of probability:

0 => 6.3211 × 10^-11
1 => 2.65486 × 10^-8
2 => 2.44911 × 10^-6
3 => 0.0000870795
4 => 0.00148579
5 => 0.0135504
6 => 0.0696342
7 => 0.204639
8 => 0.335736
9 => 0.281853
10 => 0.0930114

You're right, 8 is the most common.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment