PHP random function Mt_rand () and Rand () performance comparison

Source: Internet
Author: User
Tags comparison explode generator min rand

Example 1. Mt_rand () example

The code is as follows Copy Code

<?php
Echo Mt_rand (). "N";
Echo Mt_rand (). "N";

Echo Mt_rand (5, 15);
?>

The output of the example above is similar to the following:

1604716014
1478613278
6

Note: Since PHP 4.2.0, no longer need to use the Srand () or Mt_srand () function for the random number generator seeding, has been automatically completed.

Note: In the previous version of 3.0.7, Max means range. To get a random number in these versions and the same 5 to 15 as in the previous example, the short example is Mt_rand (5, 11).

See Mt_srand (), Mt_getrandmax (), and Rand ().


The rand () function returns a random integer.

Grammar

RAND (Min,max) parameter description
Min,max Optional. Specify the range of random numbers produced.

Description

If the optional parameter min and Max,rand () are not provided, the pseudo random integer between 0 and Rand_max is returned. For example, want a random number between 5 and 15 (including 5 and 15), with Rand (5, 15).
Tips and comments
Note: Under some platforms (for example, Windows) Rand_max is only 32768. If the desired range is greater than 32768, then specifying the min and Max parameters can generate a number greater than Rand_max, or consider replacing it with Mt_rand ().

Note: Since PHP 4.2.0, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.

Note: In the previous version of 3.0.7, Max means range. To get a random number in these versions and the same 5 to 15 as the previous example, the short example is rand (5, 11).


is Mt_rand () really 4 times times faster than Rand ()?

With this question, one side of the test while looking at the introduction of the Internet, the test is as follows.

Mt_rand () and rand () contrast test a

Test code:

The code is as follows Copy Code

<?php
$max = 100000;
$timeparts = Explode (', microtime ());
$stime = $timeparts [1].substr ($timeparts [0],1);
$i = 0;
while ($i < $max) {
Rand ();
$i + +;
}
$timeparts = Explode (', microtime ());
$etime = $timeparts [1].substr ($timeparts [0],1);
$time = $etime-$stime;
echo ' {$max} random numbers generated in {$time} seconds using rand ();
";

$timeparts = Explode (', microtime ());
$stime = $timeparts [1].substr ($timeparts [0],1);
$i = 0;
while ($i < $max) {
Mt_rand ();
$i + +;
}
$timeparts = Explode (', microtime ());
$etime = $timeparts [1].substr ($timeparts [0],1);
$time = $etime-$stime;
echo ' {$max} random numbers generated in {$time} seconds using Mt_rand ();
";
?>

Results:

First time Test
100000 random numbers generated in 0.024894952774048 using rand ();
100000 random numbers generated in 0.028925895690918 using seconds ();
Second Test
100000 random numbers generated in 0.03147292137146 using rand ();
100000 random numbers generated in 0.02997088432312 using seconds ();
Third time Test
100000 random numbers generated in 0.028102874755859 using rand ();
100000 random numbers generated in 0.02803111076355 using seconds ();
Fourth time Test
100000 random numbers generated in 0.025573015213013 using rand ();
100000 random numbers generated in 0.028030157089233 using seconds ();

This result is only a few times show results, more than a few times you will find that the two are alternating, in fact, there is not much difference between the two.

Mt_rand () and rand () contrast test two

I test the environment
Operating system: Windows XP
Apache 2.0
PHP 5.2.12
Memory 2G

  code is as follows copy code

<?php
Function microtime_float ()
{
    list $US EC, $sec) = Explode ("", Microtime ());
    return ((float) $usec + (float) $sec);
}
$time _start = Microtime_float ();
for ($i =0; $i <1000000; + + $i)
{
    rand ();
}
$time _end = Microtime_float ();
$time = $time _end-$time _start;
Echo "rand () cost $time secondsn";


$time _start = Microtime_float ();
for ($i =0; $i <1000000; + + $i)
{
    mt_rand ();
}
$time _end = Microtime_float ();
$time = $time _end-$time _start;
Echo "Mt_rand () cost $time secondsn";
?

Results:
First time
RAND () Cost 0.25919604301453 seconds
Mt_rand () Cost 0.28554391860962 seconds
Second time
RAND () Cost 0.31136202812195 seconds
Mt_rand () Cost 0.28973197937012 seconds
Third time
RAND () Cost 0.27545690536499 seconds
Mt_rand () Cost 0.27108001708984 seconds
four times
RAND () Cost 0.26263308525085 seconds
Mt_rand () Cost 0.27727103233337 seconds

The result is the same: the time is alternating between the two, in fact, there is not much difference between the two
PHP's Mt_rand () and Rand () contrast conclusion

Read a lot of other people's tests on the Internet, with Linux and the Windows environment, most people come up with the same results as mine: The two are about the same, but others have detected that Mt_rand () is 4 times times faster than Rand (), but because they didn't give a specific test environment, they couldn't tell if it was true or false. I'm still more convinced of my conclusion because I saw someone introduce Mt_rand () with Rand ():


So why does the PHP manual say that Mt_rand () is 4 times times faster than Rand ()?

This is because Mt_rand () uses the Mersenne Twister Algorythm is 1997, so 10 years ago, and Rand () the difference in Speed is (4 times times).
Since 2004, Rand () has started using algorythm, so there are no big differences in speed.

From the various tests above, there is no difference between them, but the values may change in different systems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.