PHP Coding Tips

Friday, August 10, 2012

Why not to use rand() in PHP

The rand() function in PHP is much more older, and doesn't generate such a good results as mt_rand(). What means good ? Just random. Computers cannot create real random numbers, so rand() and mt_rand() are just using some mathematic calculations to create pseudo random numbers, and mt_rand() has better and faster algorithm. Code sample:$i = rand(1,5); // don't use ! $i = mt_rand(1,5); // betterIf you want to check this, just generate a image with black and white pixels, selecting it by rand and mt_rand 0 and 1 values. You can see some patter-like areas in image created by rand()

No comments:

Post a Comment