Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rand VS MT_Rand
02-06-2015, 09:00 AM,
#1
Rand VS MT_Rand
I just wanted to throw out there that no number is truly random. There are tons of articles out there that explain this in detail. Basically, MT_Rand is NOT 4 times faster and does NOT generate better random numbers than Rand does. In fact, both are about equal and equally vulnerable. Both can be guessed by hackers. There is no solid evidence anywhere indicating that MT_Rand is faster and better than Rand, besides maybe a few claims found online. There are simple scripts that you can write that show the randomness of both functions.


--------------------------------------------------------------------------------

Script one (To show how random they really are):

<?php
header("Content-type: image/png");
$img = imagecreatetruecolor(256, 256)
or die("Error while creating the graphic.");
$white = imagecolorallocate($img, 255, 255, 255);

for ($x=0; $x<256; $x++)
{
for ($y=0; $y<256; $y++)
{
if (mt_rand(0,1) === 1)
{
imagesetpixel($img, $x, $y, $white);
}
}
}
imagepng($img);
imagedestroy($img);
?>

------------------------------------------------------

Script 2 (To show how fast they output):

<?php
$start = microtime(true);
for ($i=0; $i<1000000; $i++)
{
$random = mt_rand(0,1);
}
$end = microtime(true);

$runtimeMtRand= $end - $start;

$start = microtime(true);
for ($i=0; $i<1000000; $i++)
{
$random = rand(0,1);
}
$end = microtime(true);

$runtimeRand = $end - $start;

echo "Runtime mt_rand(): ".$runtimeMtRand." seconds!";
echo "Runtime rand(): ".$runtimeRand." seconds!";
?>

--------------------------------------------------------------------------------------------------

Both of these scripts are found here: http://en.code-bude.net/2013/01/06/php-r...is-faster/

------------------------------------------------------------------------------

Maybe I am wrong and you found better backing evidence than me, but I believe that your effort in better randomization is not finished simply by find/replacing all Rands with MT_Rands.
Reply


Messages In This Thread
Rand VS MT_Rand - by Ben Fury - 02-06-2015, 09:00 AM
RE: Rand VS MT_Rand - by Sevion - 02-06-2015, 02:36 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)