Skip to content

Instantly share code, notes, and snippets.

@qgp9
Last active January 14, 2017 13:34
Show Gist options
  • Save qgp9/53a9cc21c1ac05a5445c to your computer and use it in GitHub Desktop.
Save qgp9/53a9cc21c1ac05a5445c to your computer and use it in GitHub Desktop.
Simple decay simulation
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my @PNS = ( 3, 4, 5, 6 );
my $lambda = 0.1;
for my $PN ( @PNS ){
my $N0 = 10**$PN;
my $N = $N0;
my @nDecay;
my @nRemain;
my @ratio;
while( $N >= $N0/2 ){
my $dn = 0;
push @nRemain, $N;
for( my $i=0;$i< $N; $i++ ){
$dn++ if( rand(1) < $lambda );
}
push @nDecay, $dn;
push @ratio, $dn/$N;
$N -= $dn;
}
print "$N0\t";
map { printf "%7d/%7d/%.3f\t", $nRemain[$_], $nDecay[$_], $ratio[$_];} 0..$#nDecay;
my $avg_ratio = 0;
$avg_ratio += $_ for @ratio;
$avg_ratio /= @ratio;
printf "( %d , %.3f )\n", scalar(@nDecay), $avg_ratio;
}
1000 1000/ 92/0.092 908/ 94/0.104 814/ 85/0.104 729/ 74/0.102 655/ 77/0.118 578/ 59/0.102 519/ 47/0.091 ( 7 , 0.102 , 0.008)
10000 10000/ 953/0.095 9047/ 906/0.100 8141/ 820/0.101 7321/ 777/0.106 6544/ 664/0.101 5880/ 614/0.104 5266/ 533/0.101 ( 7 , 0.101 , 0.003)
100000 100000/ 9998/0.100 90002/ 8998/0.100 81004/ 8197/0.101 72807/ 7193/0.099 65614/ 6518/0.099 59096/ 5895/0.100 53201/ 5309/0.100 ( 7 , 0.100 , 0.001)
1000000 1000000/ 99788/0.100 900212/ 89583/0.100 810629/ 81166/0.100 729463/ 73524/0.101 655939/ 65736/0.100 590203/ 59183/0.100 531020/ 52851/0.100 ( 7 , 0.100 , 0.000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment