#!/usr/bin/perl # Benford's law number generator # Copyright (C) 2003 Lukasz Debowski # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation # (http://www.gnu.org/copyleft/gpl.html). # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # A short description: # The program generates real numbers accordingly to the Benford's law. # The arguments of the program are: # benford.pl [how many numbers] # We ignore the quality of the standard Perl pseudorandom number generator. use strict; use POSIX; my ($total)=(shift); if(!defined $total){ die ' Benford\'s law number generator Copyright (C) 2003 Lukasz Debowski The program generates real numbers accordingly to the Benford\'s law. The arguments of the program are: benford.pl [how many numbers] '; } my ($i,$j); my @count; my @tab; for $i (1..$total){ my $real=exp((rand 1)*log(10)); $real=~/^(\d)+\.(\d)(\d)/; @tab=(0,$1,$2,$3); for $j (1..$#tab){ $count[$j][$tab[$j]]++; } print "$i: $real\n"; } for $j (1..$#tab){ print "Relative frequencies of digits for position no. $j:\n"; for $i (0..$#{$count[$j]}){ my $ratio=$count[$j][$i]/$total; print "$i: $ratio\n"; } }