Yesterday I overheard a conversation about rand()
and mt_rand()
, a collegue said that both of these are predictable and you should use different functions? I was wondering, I know rand()
is predictable in some way, and after some googling. Even mt_rand()
seems to be predictable if I readed this correctly.
For this I wrote a small piece of code, which creates an image:
<?php
header("Content-type: image/png");
$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
for ($y = 0; $y < 512; $y++) {
for ($x = 0; $x < 512; $x++) {
if (rand(0, 1)) {
imagesetpixel($im, $x, $y, $white);
}
else{
imagesetpixel($im, $x, $y, $black);
}
}
}
imagepng($im); imagedestroy($im);
?>
this code outputs this image, as you can see it has some kind of pattern:
while the mt_rand()
function gave me this output:
now my question is, is mt_rand()
really that predictable, it seems pretty random to me compared to the rand()
function.