onion2k wrote:Because I've written loads of code that applies random filters to images using rand() and never had this sort of problem, and I know there are lots of pitfalls with implementing rand() that can make return results that aren't random. The same code with mt_rand() will produce random results. That doesn't make rand() the problem.
Like I said though, it's just something I suspect. I could well be wrong. We'll need to see the code to have any level of certainly.
You are partly right, but I think you misunderstood the purpose of this. The purpose was to show that mt_rand() produces
better randoms. I did not say that mt_rand() produces good randoms, or rand() produces good random, but I said mt_rand() produces
better randoms than rand(). It's a comparative. Neither of these two functions should be used blindly for cryptagraphic use as explained in
viewtopic.php?f=34&t=92271
Whether you face these kind of problems when applying image related filters or not, it depends much on the way you implement the use of rand() function. Here I tried to show the randomness of these functions with a representation of an image. I tried to exploit the unrandomness of rand() and take the advantage of it to produce a picture that looks pattern-like. If I wanted, I could use rand() to produce universums, too. But the purpose was to show off how mt_rand() produces
better randoms than rand().
Feel free to experiment this little piece of script.
Code: Select all
<?php
// Telling the browser what to handle
header('Content-Type: image/png');
// Picture size and randomness settings
$size = 512;
$randask = pi();
$randsize = log(256,$randask);
// Creating true color picture
$im = imagecreatetruecolor($size,$size);
// Background white
imagerectangle($im,0,0,$size,$size,imagecolorallocate($im,255,255,255));
// Looping through all pixels of the image
for ($a = 0;$a < $size;$a++)
{
for ($b = 0;$b < $size;$b++)
{
$mpa = array();
for ($c = 0;$c < $randsize;$c++)
$mpa[] = rand(0,$randask);
$level = $mpa[0];
for ($c = 1,$d = count($mpa);$c < $d;$c++)
$level *= $mpa[$c];
imageline($im,$a,$b,$a,$b,imagecolorallocate($im,$level-1,$level-1,$level-1));
}
}
// Output and destroy
imagepng($im);
imagedestroy($im);
?>
I used value 2 instead of pi() in the pictures shown in the top of this thread.