Dysan Posted January 17, 2008 Share Posted January 17, 2008 Hi, How do I randomly generate 6 character ID's, containing characters 1-9 and A-Z (Uppercase)? Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/ Share on other sites More sharing options...
revraz Posted January 17, 2008 Share Posted January 17, 2008 http://us3.php.net/rand May be tougher to do alpha Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442195 Share on other sites More sharing options...
Dysan Posted January 17, 2008 Author Share Posted January 17, 2008 Ah Ha, I've come up with this. How do I convert the letters to uppercase? substr(md5(uniqid(mt_rand(), true)), 0, 6); Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442197 Share on other sites More sharing options...
atlanta Posted January 17, 2008 Share Posted January 17, 2008 strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 6)); Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442198 Share on other sites More sharing options...
atlanta Posted January 17, 2008 Share Posted January 17, 2008 oh and md5 only generates a-f not a-z Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442200 Share on other sites More sharing options...
papaface Posted January 17, 2008 Share Posted January 17, 2008 Google found me: function randomPrefix($length) { $random= ""; srand((double)microtime()*1000000); $data = "AbcDE123IJKLMN67QRSTUVWXYZ"; $data .= "aBCdefghijklmn123opq45rs67tuv89wxyz"; $data .= "0FGH45OP89"; for($i = 0; $i < $length; $i++) { $random .= substr($data, (rand()%(strlen($data))), 1); } return $random; } randomPrefix(10); Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442202 Share on other sites More sharing options...
tinker Posted January 17, 2008 Share Posted January 17, 2008 this only handles characters and doesn't seed the random function, but it's cheap and cheerful: function genid($num) { $sret = ""; for($i=0;$i<$num;$i++) { $sret .= chr(rand(65, 90)); } return $sret; } print genid(6); Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442207 Share on other sites More sharing options...
kanikilu Posted January 17, 2008 Share Posted January 17, 2008 This is a quick-n-dirty alternative I came up with: <?php $num = range(0,9); // 0-9 $ascii = range(65,90); // ASCII codes for A-Z foreach ($ascii as $key => $value) { $char[$key] = chr($value); } $mixed = array_merge($num, $char); $rand_keys = array_rand($mixed, 6); foreach ($rand_keys as $value) { $charID .= $mixed[$value]; } echo $charID; ?> Note, this won't necessarily give you numbers AND alpha characters all the time, I tried it several times and twice there were all alpha's, and the others were a mix of both... Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442221 Share on other sites More sharing options...
The Little Guy Posted January 17, 2008 Share Posted January 17, 2008 This will do the trick: http://phpsnips.com/snippet.php?id=24 You can change the length to what ever you want! Quote Link to comment https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442223 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.