2007/08/06
randとmt_randのアレとか
via - 乱数 とは
何年かまえにrandとmt_randの分布をgnuplotでやった記憶があるけど、忘れてたのでもう一度。
以下のスクリプトはデータを適当な場所に生成して、gnuplotにSTDINからスクリプトとデータを書き込んだ上、xvでpngデータを表示しようとするヤツ
また、randで生成する最小、最大値は0から50の間
ちなみに、proc_openを使うことで、gunplotのような対話式のプログラムも実行できる
<?php define('__CDIR__', dirname(__FILE__)); $srand = 'srand'; $rand = 'rand'; //$srand = 'mt_srand'; //$rand = 'mt_rand'; list($usec, $sec) = explode(' ', microtime()); $srand($sec + $usec * 100000); $data = ''; for($i = 0; $i < 10000; ++$i){ $data .= implode("\t", array($i, $rand(0, 50), $rand(0, 50))) . PHP_EOL; } $filename = __CDIR__ . '/data.txt'; file_put_contents($filename, $data); $descriptorspec = array( // stdin array('pipe', 'r'), // stdout array('pipe', 'w'), // stderr array('pipe', 'w') ); $pipes = null; $process = proc_open(exec('which gnuplot'), $descriptorspec, $pipes, dirname(__FILE__)); if (is_resource($process)) { $script = array(); $script []= 'reset'; $script []= 'set term png'; $script []= 'set grid'; $script []= 'set logscale xy'; $script []= 'set title ' . $filename; $script []= 'plot "' . $filename . '" using 1:2 with points'; // 3d //$script []= 'set ticslevel 0'; //$script []= 'set view 60, 45'; //$script []= 'splot "' . $filename . '" with points'; fwrite($pipes[0], implode(PHP_EOL, $script)); fwrite($pipes[0], PHP_EOL); fclose($pipes[0]); $datafile = dirname(__FILE__) . '/data.png'; if(file_exists($datafile)){ unlink($datafile); } file_put_contents($datafile, $pipes[1]); fclose($pipes[1]); exec('xv ' . $datafile); } proc_close($process);
randの出力例
mt_randの出力例
(中略)
randもmt_randも似たようなグラフになってるなぁ。もう少しわかりやすいバラツキがあったと思うんだけど。
つづく。
Trackback
No Trackbacks
Track from Your Website
http://blog.xole.net/trackback/tb.php?id=586

Comment
No Comments