note 73077 added to function.mt-rand
| From: | zolaar at nothanks dot com | Date: | Thu, 08 Feb 2007 06:44:38 +0000 |
| Subject: | note 73077 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123108@lists.php.net to get a copy of this message | ||
a better (and likely faster) way to generate a random 6-digit hex string:
<?php
$num = mt_rand ( 0, 0xffffff ); // trust the library, love the library...
$output = sprintf ( "%06x" , $num ); // muchas smoochas to you, PHP!
return $output;
?>
The mt_rand function won't give you a number outside the bounds you asked for -- no need to
and-off the top bits -- and the sprintf function has params for length-padding & hexidecimal
output. It's likely faster because most of the work is being done by the wicked fast C
functions that PHP sits on top of, though YMMV in that dept.
----
Server IP: 64.71.164.2
Probable Submitter: 66.92.14.106
----
Manual Page -- http://www.php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/73077
Del: integrated -- https://master.php.net/note/delete/73077/integrated
Del: useless -- https://master.php.net/note/delete/73077/useless
Del: bad code -- https://master.php.net/note/delete/73077/bad+code
Del: spam -- https://master.php.net/note/delete/73077/spam
Del: non-english -- https://master.php.net/note/delete/73077/non-english
Del: in docs -- https://master.php.net/note/delete/73077/in+docs
Del: other reasons-- https://master.php.net/note/delete/73077
Reject -- https://master.php.net/note/reject/73077
Search -- https://master.php.net/manage/user-notes.php