note 107738 added to function.mt-rand
| From: | cyrax21 at gmail dot com | Date: | Thu, 01 Mar 2012 08:44:30 +0000 |
| Subject: | note 107738 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186439@lists.php.net to get a copy of this message | ||
Taking example from a note above i wanted to spot out the big difference between rand and mt_rand
when producing images using randomness as noise.
for example this is a comparation between rand and mt_rand on a 400x400 pixel png: http://oi43.tinypic.com/vwtppl.jpg
code:
<?php
header("Content-type: image/png");
$sizex=800;
$sizey=400;
$img = imagecreatetruecolor($sizex,$sizey);
$ink = imagecolorallocate($img,255,255,255);
for($i=0;$i<$sizex/2;$i++) {
for($j=0;$j<$sizey;$j++) {
imagesetpixel($img, rand(1,$sizex/2), rand(1,$sizey), $ink);
}
}
for($i=$sizex/2;$i<$sizex;$i++) {
for($j=0;$j<$sizey;$j++) {
imagesetpixel($img, mt_rand($sizex/2,$sizex), mt_rand(1,$sizey), $ink);
}
}
imagepng($img);
imagedestroy($img);
?>
the differences reduce when reducing the pixels of the image.. infact for a 100x100 pixel image the
noise produced from the rand function is much more realistic than how it is for a 400x400 image: http://oi39.tinypic.com/5k0row.jpg
(rand is on the left, mt_rand on the right)
----
Server IP: 69.147.83.197
Probable Submitter: 77.242.188.153
----
Manual Page -- http://www.php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/107738
Del: integrated -- https://master.php.net/note/delete/107738/integrated
Del: useless -- https://master.php.net/note/delete/107738/useless
Del: bad code -- https://master.php.net/note/delete/107738/bad+code
Del: spam -- https://master.php.net/note/delete/107738/spam
Del: non-english -- https://master.php.net/note/delete/107738/non-english
Del: in docs -- https://master.php.net/note/delete/107738/in+docs
Del: other reasons-- https://master.php.net/note/delete/107738
Reject -- https://master.php.net/note/reject/107738
Search -- https://master.php.net/manage/user-notes.php