note 71275 deleted from function.mt-rand by cmb
| From: | cmb@php.net | Date: | Sun, 19 Jan 2020 08:36:39 +0000 |
| Subject: | note 71275 deleted from function.mt-rand by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213913@lists.php.net to get a copy of this message | ||
Note Submitter: MagicalTux at ooKoo dot org
----
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.