Created
September 22, 2010 21:30
-
-
Save seven1m/592622 to your computer and use it in GitHub Desktop.
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
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 |
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
Ahh, I see. You are right. I updated the script to show me a spread of the face-up card count. Here is the output from 100,000 runs of the trick:
2 => 1
3 => 9
4 => 163
5 => 1388
6 => 6964
7 => 20590
8 => 33551
9 => 28064
10 => 9270
Seems it's most likely that 8 cards are face-up in each pile at the end of the trick. Cool stuff!