App-boxmuller
view release on metacpan or search on metacpan
scripts/boxmuller view on Meta::CPAN
sub LLN ( $ ) ; * LLN = * looks_like_number ; # 颿°åãé·ãããã®ã§ãçãããã
sub printErr( $ ){ print STDERR BRIGHT_RED "Option -$_[0] should have a numeric specification.\n" ; exit 1 }
$mu = $o{m} ? LLN $o{m} ? $o{m} : printErr "m" : 0 ; #m : å¹³å
$sd = $o{d} ? LLN $o{d} ? $o{d} : printErr "d" : $o{v} ? LLN $o{v} ? sqrt $o{v} : printErr "v" : 1 ; #sdï¼æ¨æºåå·®
}
sub main ( ) { # ä¹±æ°ã®åºå
sub getrand ;
sub boxmuller ( $$ ) ; #ããã¯ã¹ãã¥ã©ã¼æ³ã«ããã¬ã¦ã¹ä¹±æ°ã®ä½æ
* getrand = * boxmuller ;
* getrand = * lognormal if $o{L} ; # å¯¾æ°æ£è¦åå¸ã®æå®ããã£ãå ´åã
while ( $count < $upto ) {
sleep $o{'@'} if defined $o{'@'} ;
my $x = getrand $mu, $sd ;
$x = sprintf "% .$o{'.'}f" , $x if defined $o{'.'} ; # <-- May be efficientized.
$s1 += $x ;
$s2 += $x ** 2 ;
$count ++ ; # åºååæ°ãè¨æ°ããã®ã§åã
ãé¸ãã ã
print "$count\t" if $o{':'} ; # <-- Maybe effiecientized by other code structure.
print "$x\n" ;
}
sub boxmuller ( $$ ) { # ã¬ã¦ã¹ä¹±æ°ã®çæ
state $piW = atan2(1,1)* 8 ; #6.28=3.14*2
state $z = undef ;
do { my $t = $z ; undef $z ; return $t * $_[1] + $_[0] } if defined $z ;
my $r1 = 1 - rand ; # ; $r1 = rand until $r1 ; å¤ã0ã«ãªããã¨ã黿¢ã
my $r2 = $piW * rand ;
my $r1R = sqrt ( -2 * log $r1 );
my $r2S = sin $r2 ;
my $r2C = cos $r2 ;
( my $t , $z ) = ( $r1R * $r2S , $r1R * $r2C ) ;
return $t * $_[1] + $_[0] ;
}
sub lognormal ( $$ ) {
return exp boxmuller $_[0], $_[1] ;
}
}
sub SecondInfo( ) { # å¦çãããã¨ã«ã¤ãã¦ã®äºæ¬¡æ
å ±ãåºå
return if $o{1} ;
use FindBin qw [ $Script ] ;
my $cmd = "$Script -m $mu -d $sd" ;
$cmd .= ' -L' if $o{L} ;
print STDERR
CYAN "printed lines: ", BRIGHT_CYAN $count ,
CYAN " , used random seed: " , BRIGHT_CYAN $o{s} ,
CYAN " , elapsed seconds: " , BRIGHT_CYAN tv_interval ($time0) ,
RESET "\n" ,
CYAN "sum = " , BRIGHT_CYAN sprintf("%g", $s1 ) ,
CYAN " , squared sum = " , BRIGHT_CYAN sprintf( "%g" , $s2 ) ,
CYAN " ($cmd) " , RESET "\n" ;
}
## ãã«ãã¨ãã¼ã¸ã§ã³æ
å ±
BEGIN {
our $VERSION = 0.24 ;
$Getopt::Std::STANDARD_HELP_VERSION = 1 ;
grep { m/--help/} @ARGV and *VERSION_MESSAGE = sub {} ;
# æå㯠0.21 ãç®å®ã¨ããã
# 1.00 以ä¸ã¨ããå¿
è¦æ¡ä»¶ã¯è±èªçã®ãã«ãããã¡ãã¨åºããã¨ã
# 2.00 以ä¸ã¨ããå¿
è¦æ¡ä»¶ã¯ãã¹ãã³ã¼ããå«ããã¨ã
# 0.22 : -g inf ãæå®å¯è½ã¨ããã
# 0.23 : 説ææãåããå®ãããã
# 0.24 : è±èªã®ããã¥ã¢ã«ãPODåããã
}
sub HELP_MESSAGE {
use FindBin qw[ $Script $Bin ] ;
sub EnvJ ( ) { $ENV{LANG} =~ m/^ja_JP/ ? 1 : 0 } ; # # ja_JP.UTF-8
sub en( ) { grep ( /^en(g(i(sh?)?)?)?/i , @ARGV ) ? 1 : 0 } # English ã¨ããæååãå
é ãã2æå以ä¸ãå«ãã
sub ja( ) { grep ( /^jp$|^ja(p(a(n?)?)?)?/i , @ARGV ) ? 1 : 0 } # jp ã¾ã㯠japan ã¨ããæååãå
é ãã2æå以ä¸ãå«ãã
sub opt( ) { grep (/^opt(i(o(ns?)?)?)?$/i, @ARGV ) ? 1 : 0 } # options ã¨ããæååãå
é ãã3æå以ä¸å«ããã
sub noPOD ( ) { grep (/^no-?pod\b/i, @ARGV) ? 1 : 0 } # POD ã使ããªãã¨è¨ãæå®ãããã¦ãããã©ãã
my $jd = "JapaneseManual" ;
my $flagE = ! ja && ( en || ! EnvJ ) ; # è±èªã«ãããã©ããã®ãã©ã°
exec "perldoc $0" if $flagE && ! opt && ! noPOD ;
$ARGV[1] //= '' ;
open my $FH , '<' , $0 ;
while(<$FH>){
s/\Qboxmuller\E/$Script/gi ;
s/\$Bin/$Bin/gi ;
if ( s/^=head1\b\s*// .. s/^=cut\b\s*// ) {
if ( s/^=begin\s+$jd\b\s*// .. s/^=end\s+$jd\b\s*// xor $flagE ) {
print $_ if ! opt || m/^\s+\-/ ;
}
}
}
close $FH ;
exit 0 ;
}
=encoding utf8
=head1 NAME
boxmuller
=head1 VERSION
0.24 -- 2018-07-03
=head1 SYNOPSIS
boxmuller [B<-m> mean] [B<-v> variance | B<-d> standard_deviation]
[B<-g> how_many_you_want] [B<-.> digits_after_decimal_point] [B<-s> random_seed]
[B<-L>(log normal)] [B<-@> seconds] [B<-1>] [B<-:>]
boxmuller [B<--help> [ja|en] [opt] [nopod]] [B<--version>]
=head1 DESCRIPTION
Generates Gaussian random variables by Box-Muller method.
The used random seed and the sums of the generated numbers and the square of them are also
provided to STDERR.
=head1 OPTION
=over 4
=item -m N
Population B<Mean (average)>. Default value is 0.
=item -d N
Population B<Standard Deviation>. Default value is 1.
=item -v N
Population B<Variance>. Default value is 1. If -d is given, -v would be nullified.
=item -. N
The digits to be shown after the decimal point. Without this specification
14 digits after the decimal point may be shown.
=item -g N
How many numbers to be produced. Default value is 6. "Inf" (Infinity) can be given.
=item -s N
Random B<seed> specification. The residual divided by 2**32 is essential.
=item -L
Outputs variables from the B<log normal distribution> instead of the normal distribution.
=item -1
Only output the random number without other secondary information.
=item -:
Attaches serial number beginning from 1.
=item -@ N
Waiting time in B<seconds> for each output line, that can be spedicifed 6 digits after the decimal points
( run in 3.072 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )