Page 1 of 1

rand() or mt_rand()... which is better to use?

Posted: Mon Aug 27, 2007 5:59 pm
by Citizen
In the manual, it appears that both have the exact same funciton, and mt_rand() is advertised as being faster.

Is mt_rand() really faster? Is it *always* better to use?

Posted: Mon Aug 27, 2007 6:46 pm
by Weirdan
mt_rand() has predictable characteristics (speed, range of values, distribution) while rand() depends on implementation provided by your operating system (libc)

Re: rand() or mt_rand()... which is better to use?

Posted: Mon Aug 27, 2007 8:33 pm
by AKA Panama Jack
Citizen wrote:Is mt_rand() really faster? Is it *always* better to use?
Yes and Yes

We have a browser based game that uses quite a few random numbers and after quite a bit of testing we found that rand() had a bad habit of producing the same number chains even though it reseeds the generator automatically with PHP 4.2 and higher. mt_rand() tended to give a wider range of numbers with far less tendency to repeat patterns.

Posted: Mon Aug 27, 2007 8:56 pm
by s.dot
If you have multiple requests within a short time period, rand() tends to give out far more repeat numbers.

EDIT| And yes, mt_rand() is faster.

Posted: Mon Aug 27, 2007 9:31 pm
by Z3RO21
Yup mt_rand() is faster there was a speed test post done on it (I think it was on this site) and it showed that mt_rand was faster and also had far less repeated characters. Does anyone know what they look like at the c level?

Posted: Tue Aug 28, 2007 4:45 am
by xpgeek
Write you own tests for checking productivity of each fucntions.
I suggest write loop on 1 million iteration, but you test may be more specific to you tasks.
Try do it and you would have answers. :wink: