#!/usr/bin/perl use 5.014 ; use strict ; use warnings ; use Getopt::Std ; getopts '/:~.:1g:s:1y:', \my %o ; #or HELP_MESSAGE () ; use Term::ANSIColor qw[ :constants ] ; $Term::ANSIColor::AUTORESET = 1 ; # 乱数を生æˆã™ã‚‹é–¢æ•°è¨å®š sub rand_gen ( ) ; my $osep = $o{'/'} // "\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{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 ) { print join $osep , map { rand_gen } 1 .. $c ; print "\n" ; } } sub info ( ) { exit 0 if $o{1} ; $0 =~ s|.*/||; print STDERR CYAN "used random seed = $o{s} ($0 -s $o{s} -g ${r}x${c} -y $d..$u" . (defined $o{'.'} ? " -. $o{'.'}" : '' ) .")\n" ; } sub VERSION_MESSAGE {} 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 saikoro -- a random number/matrix generator from uniform distributions. Program name : saikoro ('=bin=') saikoro -g X,Y -y L..U # X,Y,L,U are all numbers. Generates random uniform variable. Discrete/uniform can be specified. Usage Example : saikoro # Outputs 12 random numbers from {1,2,3,4,5,6} horizontally. saikoro -~ # Outputs 12 random numbers from {1,2,3,4,5,6} vertically. saikoro -g 5,8 -y 0,100 # 5 columuns times 8 rows numbers from {0,1,2,..100}. saikoro -g 5,8 -. 3 # Continuous random variables with 3 digits after decimal points. オプション: -g N ; Get N random variables. -g N1,N2 ; Get N1 times N2 variables. N1 for vertical, N2 for horizontal. -~ ; Number specifications N1 and N2 are reversed. -y N1,N2 ; Limit the values in [N1,N2]. Form "-y N1..N2" is also allowed. -. N : Switch to continuous from discrete. N digits after decimal points by rounding. -1 : Switch to no secondary information that would be output to STDOUT. (Quiet) -s N : Random seeed specification. Essentially the residual divided by 2^32 is used. -/ char : Specifies the horizontal separator character. --help : Print this online help manual of this command "'=SCRIPT='". Similar to "perldoc `which [-t] '=SCRIPT='` ". --help opt ..or.. --help options : Only shows the option helps. It is easy to read when you are in very necessary. --help ja : Shows Japanese online help manual. ; "saikoro --help ja" ã§æ—¥æœ¬èªžã®ã‚ªãƒ³ãƒ©ã‚¤ãƒ³ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã‚’表示ã—ã¾ã™ã€‚ --help nopod : Print this online manual using the code insdide this program without using the function of Perl POD. # This program has been made since 2016-03-04(Fri) by Toshiyuki Shimono, as a part of TSV hacking toolset for table data. =begin JapaneseManual プãƒã‚°ãƒ©ãƒ å : '=script=' ('=bin=') '=script=' -g 列数,行数 -y 下é™,ä¸Šé™ ä¹±æ•°ã¾ãŸã¯ä¹±æ•°è¡Œåˆ—を生æˆã€‚å„è¦ç´ 㯠1以上ã§ä¸Šé™ã‚’超ãˆãªã„æ•´æ•°ã«ãªã‚‹ã€‚ オプション: -g N ; 乱数をNè¡Œ1列ã®å½¢ã§å‡ºåŠ›ã™ã‚‹ã€‚ -g N1,N2 ; 乱数をN1è¡ŒN2列ã®å½¢ã§å‡ºåŠ›ã™ã‚‹ã€‚区切り文å—㯠, ã®ä»£ã‚ã‚Šã«ã€xã§ã‚‚良ã„。 (Get) -~ ; 出力ã™ã‚‹è¡Œæ•°ã¨åˆ—æ•°ã®æŒ‡å®šã‚’å転ã™ã‚‹ã€‚ -y N1,N2 ; 乱数ã®å€¤ã‚’n1ã¨n2ã®ç¯„囲ã¨ã™ã‚‹ã€‚ -y N1..N2 ã®å½¢å¼ã‚‚許容ã•ã‚Œã‚‹ã€‚ -. N : 出力ã¯æ•´æ•°ã§ã¯ãªãã€é€£ç¶šåž‹ã®ä¸€æ§˜ä¹±æ•°ã«ãªã‚‹ã€‚å°æ•°ç‚¹ä»¥ä¸‹Næ¡ãŒå‡ºåŠ›ã•ã‚Œã‚‹ã€‚å››æ¨äº”å…¥(sprintfã«ä¾å˜)。 -s N : ランダムシードã®æŒ‡å®šã€‚2^32 (ç´„43å„„<10^10) ã§å‰²ã£ãŸå‰°ä½™ãŒæ¸¡ã•ã‚Œã‚‹ã€‚ -1 : ランダムシードã®æƒ…å ±ã‚’å‡ºåŠ›ã—ãªã„。 -/ char : 出力ã®(横方å‘ã®)区切り文å—ã®æŒ‡å®š --help : ã“ã® $0 ã®ãƒ˜ãƒ«ãƒ—メッセージを出ã™ã€‚ perldoc -t $0 | cat ã§ã‚‚ã»ã¼åŒã˜ã€‚ --help opt : オプションã®ã¿ã®ãƒ˜ãƒ«ãƒ—を出ã™ã€‚opt以外ã§ã‚‚ options ã¨å…ˆé ãŒ1æ–‡å—以上一致ã™ã‚Œã°è‰¯ã„。 --help en : 英語版ã®ã‚ªãƒ³ãƒ©ã‚¤ãƒ³ãƒ˜ãƒ«ãƒ—マニュアルを出力。Online help in English version. 例: '=script=' -y 1000 -g 5,8 # 縦8è¡Œ 横5列ã§è¦ç´ ㌠1 ã‹ã‚‰ 1000 ã¾ã§ã®æ•´æ•°ã®ä¸€æ§˜ä¹±æ•°ã® 乱数行列を生æˆã™ã‚‹ã€‚ '=script=' -q -g 2,3 -y1..5 # -q ã«ã‚ˆã‚Šãƒ©ãƒ³ãƒ€ãƒ シードã®æƒ…å ±ã‚’è¡¨ç¤ºã—ãªã„。 '=script=' -u # 実数(æ•´æ•°ã¨ã¯é™ã‚‰ãªã„) 一様乱数ã®å‡ºåŠ›ã€‚ '=script=' -s 123 -u -d50 -g1,10 -y1 ; # å°æ•°ç‚¹ä»¥ä¸‹50æ¡è¡¨ç¤ºã•ã‚Œã‚‹ã¯ãšã€‚2進数48æ¡ã§å†…部ãŒå‹•ã事ãŒåˆ†ã‹ã‚‹ã€‚ # ã“ã®ãƒ–ãƒã‚°ãƒ©ãƒ 㯠2016å¹´3月4æ—¥(金)ã‹ã‚‰è¡¨å½¢å¼ãƒ‡ãƒ¼ã‚¿ã«å¯¾ã™ã‚‹é“具作りã®ä¸€ç’°ã¨ã—ã¦ã€ä¸‹é‡Žå¯¿ä¹‹ãŒä½œæˆã—ãŸã‚‚ã®ã§ã‚る。 =end JapaneseManual =cut