I need to generate 3 random strings. 1) A random string that's 250 charters, the random string needs to have uppercase, lowercase, numbers. 2) A random string that's 200 charters, the random string needs to have uppercase, lowercase, numbers. 3) A random string that's 150 charters, the random string needs to have uppercase, lowercase, numbers. Thanks, Chris
<? function rand_str1($len) { // random string $str = ''; for ($i=0; $i<26; $i++) $str .= chr(ord('A')+$i).chr(ord('a')+$i); for ($i=0; $i<10; $i++) $str .= $i; $ret = ''; for ($i=0; $i<$len; $i++) $ret .= substr($str, rand(0, strlen($str)-1), 1); return $ret; } function rand_str2($len) { // simple but only lowercase $ret = ''; while (strlen($ret)<$len) $ret .= md5(rand()); return substr($ret, 0, $len); } echo rand_str1(200); echo "<BR>"; echo rand_str2(200); ?> Code (markup): You can use any of two functions above. The 2nd one is simple but only gives lowercase strings, and the 1st one gives upper and lower strings. Both add numeric chars. Does it suite your needs?
Same thing as @livedatesearch, just a little different: <?php function genRandomString($length) { $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUBWXYZ"; $string = ""; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; } return $string; } $string1 = genRandomString(250); $string2 = genRandomString(200); $string3 = genRandomString(150); echo $string1; // 2 or 3 where needed. ?> PHP:
try this.. <?php $characters = 250; $letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); $output = ''; for($i=1; $i<=$characters; $i++) { switch(rand(0,1)) { case 0: $output.= rand(0,9); break; case 1: $letter = $letters[array_rand($letters)]; $output.= rand(0,1)?strtoupper($letter):$letter; break; } } echo $output; ?> PHP: just change the $characters to change the number of characters needed..
Whatever function you use, use mt_rand instead of rand. http://php.net/manual/en/function.mt-rand.php Also, it's not really probably, but you could theoretically have colissions with this script. If you need something random and unique, make a hash based on a timestamp or something that doesn't repeat. This is one, I use for generating random strings. function random_string($length=16) { $string = ''; $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for ($i = 1; $i < $length; $i++) { $char = substr($possible, mt_rand(0, strlen($possible) - 1), 1); $string .= $char; } return $string; } echo random_string(250); echo random_string(200); echo random_string(150); PHP:
Avoiding for loop in here, works almost same way, but expected to be a bit quick. <?php function random_string($length) { $possible = str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); while(strlen($possible) < $length) { $possible .= str_shuffle($possible); } return substr(str_shuffle($possible), 0, $length); } echo random_string(250)."\n"; echo random_string(200)."\n"; echo random_string(150)."\n"; ?> PHP: Hope it helps.