PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
mt_rand() has predictable characteristics (speed, range of values, distribution) while rand() depends on implementation provided by your operating system (libc)
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.
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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
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?
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.