Followup: PHP: rand() vs. mt_rand()

My past comparison of rand and mt_rand only compared the speed, and I saw very little difference. This time I’ll compare how truly random the numbers each function produces are.

The code used to test this is:

<?php
$img 
imagecreatetruecolor(512,512);
$i 0;
while(
$i 512*512) {
imagesetpixel(
$img,
mt_rand(0,512),
mt_rand(0,512),
imagecolorallocatealpha(
$img,
mt_rand(0,255),
mt_rand(0,255),
mt_rand(0,255),
mt_rand(0,127)
)
);
$i++;
}
imagepng($img,”mt_rand.png”);
imagedestroy($img);
$img imagecreatetruecolor(512,512);
$i 0;
while(
$i 512*512) {
imagesetpixel(
$img,
rand(0,512),
rand(0,512),
imagecolorallocatealpha(
$img,
rand(0,255),
rand(0,255),
rand(0,255),
rand(0,127)
)
);
$i++;
}
imagepng($img,”rand.png”);
imagedestroy($img);
?>

On Linux (Slackware 10.2), the results looked fine, two seemingly random graphs were produced. On Windows (XP), however, the results were drastically different between functions:

rand

mt_rand

So, if your code relies on random numbers, and you plan on using it on multiple operating systems, mt_rand appears to be the way to go.

This entry was posted in Code, PHP and tagged , , , . Bookmark the permalink.

5 Responses to Followup: PHP: rand() vs. mt_rand()

  1. T.J. says:

    I fixed the messed up bbcode in this article. Everything looks good now.

  2. Google rocks says:

    Nice comparison test.

    Graphing out the results is a straight forward way to show the randomness of both function.

  3. MG says:

    this is a great way to visualize it. I wonder if you shut down the program and restart it, if it will produce the same image.

    Some random functions in other languages have this problem. They may produce seemingly very random numbers, but will produce the SAME sequence of “random” numbers each time the program is run.

  4. Frank says:

    Okay! I already knew that windowse sucks, but this? :)

    I wounder if it is the fault of php or the underlying libraries.

    @MG:
    I had to test this on my Ubuntu-Laptop ( 9.10 Karmic Koala, 32bit, PHP-CLI 5.2.10-2ubuntu6.3 with Suhosin-Patch 0.9.7),
    I added these lines at the end of the file:

    echo “Some M-D-fives:\n”;

    echo md5_file(“mt_rand.png”) . “\n”;

    echo md5_file(“rand.png”) . “\n”;

    first time i got:
    3511cf6031b758c20102271bb2d76b55
    109d0d1f698ef634e0570c32cfe969ab
    Second time:
    227111610382830abe9efa2808404900
    bd670e07f3af2f2fdb10459d62806be6

    So it seems like it’s producing different images on my system, gonna check the server soon.

  5. Arduinology says:

    I got very different results running in a native linux environment.

    http://arduinology.com/test/mt_rand3.png
    http://arduinology.com/test/rand3.png

    It is worth noting that mt_rand() ran on average a fraction faster than rand().
    Array
    (
    [0] => Array
    (
    [mt_rand] => 0.549784898758
    [rand] => 0.563598871231
    )

    [1] => Array
    (
    [mt_rand] => 0.557963132858
    [rand] => 0.548408031464
    )

    [2] => Array
    (
    [mt_rand] => 0.609528064728
    [rand] => 0.883399963379
    )

    [3] => Array
    (
    [mt_rand] => 0.568523168564
    [rand] => 0.590033054352
    )

    [4] => Array
    (
    [mt_rand] => 0.596122026443
    [rand] => 0.585232019424
    )

    )

    I would still use mt_rand() as it is just oh so slightly quicker.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>