note 113214 added to function.openssl-random-pseudo-bytes

From: Date: Fri, 13 Sep 2013 07:25:32 +0000
Subject: note 113214 added to function.openssl-random-pseudo-bytes
Groups: php.notes 
Request: Send a blank email to php-notes+get-196248@lists.php.net to get a copy of this message
Here's an example to show the distribution of random numbers as an image. Credit to Hayley Watson at the mt_rand page for the original comparison between rand and mt_rand. rand is red, mt_rand is green and openssl_random_pseudo_bytes is blue. NOTE: This is only a basic representation of the distribution of the data. Has nothing to do with the strength of the algorithms or their reliability. <?php header("Content-type: image/png"); $sizex=800; $sizey=800; $img = imagecreatetruecolor(3 * $sizex,$sizey); $r = imagecolorallocate($img,255, 0, 0); $g = imagecolorallocate($img,0, 255, 0); $b = imagecolorallocate($img,0, 0, 255); imagefilledrectangle($img, 0, 0, 3 * $sizex, $sizey, imagecolorallocate($img, 255, 255, 255)); $p = 0; for($i=0; $i < 100000; $i++) { $np = rand(0,$sizex); imagesetpixel($img, $p, $np, $r); $p = $np; } $p = 0; for($i=0; $i < 100000; $i++) { $np = mt_rand(0,$sizex); imagesetpixel($img, $p + $sizex, $np, $g); $p = $np; } $p = 0; for($i=0; $i < 100000; $i++) { $np = floor($sizex*(hexdec(bin2hex(openssl_random_pseudo_bytes(4)))/0xffffffff)); imagesetpixel($img, $p + (2*$sizex), $np, $b); $p = $np; } imagepng($img); imagedestroy($img); ?> ---- Server IP: 69.147.83.199 Probable Submitter: 84.124.50.10 ---- Manual Page -- http://php.net/manual/en/function.openssl-random-pseudo-bytes.php Edit -- https://master.php.net/note/edit/113214 Del: integrated -- https://master.php.net/note/delete/113214/integrated Del: useless -- https://master.php.net/note/delete/113214/useless Del: bad code -- https://master.php.net/note/delete/113214/bad+code Del: spam -- https://master.php.net/note/delete/113214/spam Del: non-english -- https://master.php.net/note/delete/113214/non-english Del: in docs -- https://master.php.net/note/delete/113214/in+docs Del: other reasons-- https://master.php.net/note/delete/113214 Reject -- https://master.php.net/note/reject/113214 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196248) next »