jd2007 Posted July 15, 2007 Share Posted July 15, 2007 How to generate random letters and numbers with php ? Quote Link to comment Share on other sites More sharing options...
metrostars Posted July 15, 2007 Share Posted July 15, 2007 For numbers: rand(0, 15); where 0 is the lowest and 15 is the highest number. For random letters i usually do something like: md5(rand(0, 10000)); OR: str_shuffle(abcdefghijklmnopqrstuvwxyz); is also a possibility. Quote Link to comment Share on other sites More sharing options...
chigley Posted July 15, 2007 Share Posted July 15, 2007 MD5 also contains letters so you can just do an md5 of a random number and use substr() to cut it to the right length! Quote Link to comment Share on other sites More sharing options...
jd2007 Posted July 15, 2007 Author Share Posted July 15, 2007 thanks, guys ! Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted July 15, 2007 Share Posted July 15, 2007 I always do something like this when creating a string of a certain length comprised of certain characters: //Length of the random string $length = 8; //Specify which characters you want in your string. Here it's letters (a-z) $characters = range(a-z); //Create the string with a while loop $i = 1; while($i <= $length) { $string .= $characters[rand(0,count($characters))]; $i++; } Quote Link to comment Share on other sites More sharing options...
lewis987 Posted July 15, 2007 Share Posted July 15, 2007 or you can use this simple code: $rand1 = rand(3,86); $rand2 = rand(25,66); $random_number = mktime() * $rand1 + $rand1 * $rand2; Quote Link to comment 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.