note 16549 added to function.mt-srand

From: Date: Sat, 03 Nov 2001 22:23:28 +0000
Subject: note 16549 added to function.mt-srand
Groups: php.notes 
Request: Send a blank email to php-notes+get-20402@lists.php.net to get a copy of this message
Since I've seen this problem in so many other manual comments I'm writing a note here. Basically, calling mt_srand() every time you call mt_rand() defeats the whole purpose of a generator like the Mersenne Twister, and will greatly reduce the quality of the number stream you produce. For cryptographic or security purposes this is A Bad Thing. <br> DON'T do this: <br> function bad_pick_random_number($min, $max) { mt_srand(); return mt_rand($min, $max); } <br> Instead, call mt_srand() *once* early in the execution of your program and pick your numbers separately after that: <br> function good_pick_random_number($min, $max) { return mt_rand($min, $max); } <br> Of course, if you're doing that sort of thing you might as well just call mt_rand() directly. -- http://www.php.net/manual/en/function.mt-srand.php http://master.php.net/manage/user-notes.php?action=edit+16549 http://master.php.net/manage/user-notes.php?action=delete+16549 http://master.php.net/manage/user-notes.php?action=reject+16549

« previous php.notes (#20402) next »