App-saikoro

 view release on metacpan or  search on metacpan

saikoro  view on Meta::CPAN

#!/usr/bin/perl 
use 5.014 ; use warnings ;
use Getopt::Std ; getopts 'i:~.:2:g:s:y:' => \my %o ; #or HELP_MESSAGE () ;
use Term::ANSIColor qw[ :constants ] ; $Term::ANSIColor::AUTORESET = 1 ; 
use feature qw [ say ] ;
# 乱数を生成する関数設定
sub rand_gen ( ) ; 

my $osep = $o{i} // "\t" ; # 出力の横方向の区切り文字
my ($r,$c) ; # 列数 と 行数
my ($d,$u) ; # 下限 と 上限

& init ; 
& main ;
& info ;
exit 0 ;

sub init ( ) {
    # 乱数シードの指定
    $o{s} = defined $o{s} ? srand $o{s} : srand ; 

     # 列数と行数
    ( $r, $c ) = split /[,x]+/o , $o{g} // '' , 2 ; 
    $c //= 1 ; # 列数の未指定値
    $r //= 12 ; # 行数の未指定値
    ( $r, $c ) = ( $c, $r ) if $o{'~'} ; 

    # 生成する乱数の範囲
    ( $d , $u ) = split /(?:,|\.\.)/ , $o{y} // '' , 2 ;
    $d //= 6 ;  # さいころの目の最大値 数の指定が1個もない場合もある。
    ($d,$u) = (defined$o{'.'}?0:1, $d) if ! defined $u ; # 範囲指定がd..uの形式で無いなら -uのユニフォーム指定に従い0..nまたは1..n
    # 使用する乱数生成関数の設定
    * rand_gen = defined $o{'.'}? * rand_gen_unif_fmt : * rand_gen_int ; 
    sub rand_gen_int { state $base=$d; state $range = $u-$d+1 ; $base + int rand $range } ; 
    sub rand_gen_unif { state $base=$d; state $range =  $u-$d ; $base + rand $range } ; 
    sub rand_gen_unif_fmt { state $fmt="% 0.$o{'.'}f" ; sprintf $fmt , rand_gen_unif } ; 
}

sub main ( ) {
    for ( 1 .. $r ) { 
        say join $osep , map { rand_gen } 1 .. $c ;
    }
}

sub info ( ) {
    exit 0 if 0 eq ($o{2}//'') ;
    $0 =~ s|.*/||;
    say STDERR BOLD DARK ITALIC CYAN 
      "Used random seed = $o{s}  [$0 -g ${r}x${c} -y $d..$u" .  (defined $o{'.'} ? " -. $o{'.'}" : '' ) ."]" ;
}

## ヘルプとバージョン情報
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 : 英文マニュアルをPOD形式にする。
   # 0.23 : 英文マニュアルのPOD形式の部分をさらに増やした。
   # 0.24 : 英文マニュアルを少し書き加え。
}  
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-?p(od?)?\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/\Q'=script='\E/$Script/gi ;
        s/\Q'=bin='\E/$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

saikoro 

=head1 VERSION 

0.24 (2018-07-10)

=head1 SYNOPSIS

saikoro B<[-g N1[,N2]] [-y N3[..N4]]>   # N1, N2, N3 and N4 are all numbers.

=head1 DESCRIPTION

A random number(matrix) generator from uniform distributions.
Generates random uniform variable. Discrete/uniform can be specified.

=head1 OPTION

=over 4

=item B<-g N1>

Get N1 random variables. N1 is a positive interger.

=item B<-g N1,N2>

Get N1 times N2 variables. N1 for vertical, N2 for horizontal.
The form "B<-g N1xN2>" is allowed.

=item B<-~   >

The number specifications N1 and N2 are reversed, so N1 for horizontal, N2 for vertical.

=item B<-y N3,N4>

Limit the values into the number interval [N3,N4]. 
The form "B<-y N3..N4>" is also allowed. 

=item B<-y N3> 

Similar to -y 1..N3. When -. N is simultaneously set, similar to -y 0..N3. 

=item B<-. N>

N digits after decimal points by rounding(, switching from merely discrete integers to continuous). 
If N=0, the outputs are integers.

=item B<-2 0>

Switch to no secondary information that would be output to STDOUT. 

=item B<-s N>

Random seed specification. Essentially the residual divided by 2**32 is used.

=item B<-i char>

Specifies the horizontal separator character.

=item B<--help>

Print this online help manual of this command "saikoro". Similar to "perldoc `which [-t] saikoro` ".

=item B<--help opt>

Only shows the option helps. It is easy to read when you are in very necessary.



( run in 1.751 second using v1.01-cache-2.11-cpan-d7f47b0818f )