PHP: rand() vs. mt_rand()

PHP: rand() vs. mt_rand() - php,rand,mt_rand,test The documentation for mt_rand() says that it produces numbers four times faster than the standard rand(). So, being that I use random numbers in my code often, I decided to test this.

Note: There is a followup to this article that discusses the randomness of the numbers generated by the two functions on different operating systems.

The test 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();<br/>";
	

	
$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();<br/>";
	
?>
	


The Results:

On my laptop running a slightly outdated version of PHP 4.4 (around 4.4.0 if I remember correctly), the results were not as I expected. Most of the time, rand() was faster than mt_rand() by about 0.02 seconds. Of course, this isn't a big performance difference, but it's not the four times faster that was promised.

Sample:
100000 random numbers generated in 0.127249002457 seconds using rand();
100000 random numbers generated in 0.140385866165 seconds using mt_rand();

On TechnoServ, the results were closer. Usually rand() won by 0.01 seconds or less. Once again, not the supposed "four times faster."

Sample:
100000 random numbers generated in 0.11243796348572 seconds using rand();
100000 random numbers generated in 0.11861705780029 seconds using mt_rand();

So, despite the PHP documentation, rand() and mt_rand() are about the same speed, rand is just slightly faster.
Tags: php, rand, mt_rand, test

Comments

This is because mt_rand() uses the Mersenne Twister algorythm (1997), so 10 years ago, the speed difference was effective (4 times faster).
Since 2004 or 2003, don't remember, the algorythm for rand() has been substituted and now there are no much difference in speed ;)
By Filth 11/09/07 05:05:23

nice information.
By pete 06/21/08 03:46:12

I tested it at my laptop with latest php-cli ( thats in the Ubuntu repository ):
10000000 random numbers generated in 3.07709908485 seconds using rand();
10000000 random numbers generated in 2.68754291534 seconds using mt_rand();

Observe that I tested with 100 times more of random numbers :)
By Frank 12/30/09 03:42:48

I got these results while trying on WIN7

100000 random numbers generated in 0.062403202056885 seconds using rand();
100000 random numbers generated in 0.028571844100952 seconds using mt_rand();

It shows that mt_rand(); is 4 times faster.
By Xerrion 01/22/10 11:02:44

So even PHP is resorting to shitty products that only work as promised on Win7 now? Tsk tsk tsk...
By Mutant 06/01/10 02:35:44

well to conclude
it really depends on your system
greetz :D
By ocwil 07/01/10 09:43:08

Using WinXP SP3 (Or later?)
PHP Version 5.3.1
I got a 0.02 faster speed for rand().

100000 random numbers generated in 0.043914079666138 seconds using rand();
100000 random numbers generated in 0.064139127731323 seconds using mt_rand();

Using 100x the original max, I have a 0.2 second difference, with rand() being the faster.

@Xerrion: The speeds you posted, mt_rand is half of rand().

By cmd276 07/10/10 07:51:13

on my live server with opensuse 64 bit and php 5.2.12. i got these results:

100000 random numbers generated in 0.018066883087158 seconds using rand();
100000 random numbers generated in 0.018900871276855 seconds using mt_rand();

i tried that multiple times. rand() was the fastest one every time.
By alex 08/22/10 02:26:16

Add Comment

Please note that your IP address is logged for security purposes and that if you spam, your IP will be blocked from accessing this site.
Name:
URL:
Comment:
The following bbcode is allowed: [b] [i] [s] [url=(url)]link title[/url] [code]

ReCAPTCHA: