| ||||||
|
So you want real random numbers in PHP? rand & srand vs. mt_rand & mt_srand
If you are like me you probably once upon a time spotted that PHP had a random number generating function called rand(), decided to save it to your memory, and left it at that. After all, a random number is a random number. Why would a programming language need more than one random number generating function? Well, apparently the creators of PHP did once upon a time decide one random number generating function is not enough. Instead they decided to leave rand() for the simpleton peasants like myself: echo rand(0, 99999); Then for the more curious programmer they invented a fun function called srand(), which as far as I can tell acts like the lottery ball machine, shooting numbers all over the place before rand() lets them out of their cage: srand((double)microtime()*1000000); echo rand(0, 99999); Now most developers would leave it at that and celebrate a job well done. But I guess a true PHP expert needs more. Like a good wine this is one for your connoisseur: mt_rand()! This function is stronger, faster and apparently has the looks of a Greek god. And while rand() can only deliver random numbers between 0 and 32767, mt_rand() can do some true heavy lifting in the number generating world: 0 - 2147483647. Wow, anyone who has been using simple little rand() all these years will feel like crying right about now. echo mt_rand(0, 99999); It gets worse though. For some apparently mt_rand() alone was not enough. For the super geek out there something special and out of the ordinary was needed: mt_srand()! Some say numbers simply fall out of existence itself when this baby is put into motion. Others believe it could actually tear a black hole into existence! mt_srand((double)microtime()*1000000); echo mt_rand(0, 100); Well, I tried it, and no black hole was produced. In fact, using the following code I let these functions go at each other, and I can't say there was that much of a difference. I am sure that if a comparison was done on a larger scale a significant difference would become apparent thanks to mt_rand() and mt_srands()'s far more complex algorithms. But on the other hand, think of how much quicker it is to simply type rand()! You can save yourself a full split second there! Now that is fast! <?php echo "<b>rand() without srand()</b><br><br>"; for($i = 0; $i != 5; $i++) { echo rand(0, 100)."<br><br>"; } srand((double)microtime()*1000000); echo "<b>rand() with srand()</b><br><br>"; for($i = 0; $i != 5; $i++) { echo rand(0, 100)."<br><br>"; } echo "<b>mt_rand() without mt_srand()</b><br><br>"; for($i = 0; $i != 5; $i++) { echo mt_rand(0, 100)."<br><br>"; } mt_srand((double)microtime()*1000000); echo "<b>mt_rand() with mt_srand()</b><br><br>"; for($i = 0; $i != 5; $i++) { echo mt_rand(0, 100)."<br><br>"; } ?> The results: rand() without srand() 51 31 69 21 72 rand() with srand() 6 62 45 28 61 mt_rand() without mt_srand() 11 81 23 8 40 mt_rand() with mt_srand() 95 80 23 38 14
|