note 71275 added to function.mt-rand
| From: | MagicalTux at ooKoo dot org | Date: | Mon, 20 Nov 2006 15:34:26 +0000 |
| Subject: | note 71275 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119906@lists.php.net to get a copy of this message | ||
In answer to David and pHp_n00b, about generating a random 6 characters hexadecimal string...
Well, the easiest solution would be :
<?php
$rand = mt_rand(0x000000, 0xffffff); // generate a random number between 0 and 0xffffff
$rand = dechex($rand & 0xffffff); // make sure we're not over 0xffffff, which
shouldn't happen anyway
$rand = str_pad($rand, 6, '0', STR_PAD_LEFT); // add zeroes in front of the generated
string
echo $rand;
?>
Some examples of generated stuff :
8514d2
3188ae
028f3e
444397
1de508
071662
You can easily make a function from this code.
----
Server IP: 195.221.21.36
Probable Submitter: 62.73.5.156
----
Manual Page -- http://www.php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/71275
Del: integrated -- https://master.php.net/note/delete/71275/integrated
Del: useless -- https://master.php.net/note/delete/71275/useless
Del: bad code -- https://master.php.net/note/delete/71275/bad+code
Del: spam -- https://master.php.net/note/delete/71275/spam
Del: non-english -- https://master.php.net/note/delete/71275/non-english
Del: in docs -- https://master.php.net/note/delete/71275/in+docs
Del: other reasons-- https://master.php.net/note/delete/71275
Reject -- https://master.php.net/note/reject/71275
Search -- https://master.php.net/manage/user-notes.php