Created
September 16, 2011 07:01
-
-
Save masak/1221405 to your computer and use it in GitHub Desktop.
How many matches are left in the non-empty box?
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
#! /usr/bin/perl | |
# Simulates the distribution of the age-old question: when matches are | |
# picked randomly out of two matchboxes each of which initially has 40 | |
# matches in it until one of them is empty, how many matches are left | |
# in the other one? | |
use 5.010; | |
use strict; | |
use warnings; | |
my $MATCHES = 40; | |
my $RUNS = 1_000_000; | |
my $WIDTH = 70; | |
my @hist; | |
for (1..$RUNS) { | |
my @boxes = ($MATCHES) x 2; | |
while ($boxes[0] && $boxes[1]) { | |
--$boxes[int(rand() * 2)]; | |
} | |
++$hist[ $boxes[0] + $boxes[1] ]; | |
} | |
sub max { my $max = shift // 0; for (@_) { $max = $_ if $max < $_ }; $max } | |
my $max = max(@hist); | |
for my $n (1..@hist-1) { | |
my $v = $hist[$n] // 0; | |
say sprintf "%2d %5s %s", $n, $v, '*' x ($WIDTH * $v / $max); | |
} |
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
1 89805 ********************************************************************* | |
2 90285 ********************************************************************** | |
3 88995 ******************************************************************** | |
4 86558 ******************************************************************* | |
5 82928 **************************************************************** | |
6 78896 ************************************************************* | |
7 72912 ******************************************************** | |
8 67099 **************************************************** | |
9 60645 *********************************************** | |
10 53407 ***************************************** | |
11 46289 *********************************** | |
12 39566 ****************************** | |
13 33564 ************************** | |
14 27338 ********************* | |
15 21916 **************** | |
16 16808 ************* | |
17 12990 ********** | |
18 9656 ******* | |
19 6905 ***** | |
20 4918 *** | |
21 3181 ** | |
22 2158 * | |
23 1349 * | |
24 871 | |
25 461 | |
26 261 | |
27 132 | |
28 52 | |
29 32 | |
30 16 | |
31 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment