note 107738 modified in function.mt-rand by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:38:24 +0000 |
| Subject: | note 107738 modified in function.mt-rand by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193171@lists.php.net to get a copy of this message | ||
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)
--was--
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)
http://php.net/manual/en/function.mt-rand.php