ShaolinF Posted November 24, 2009 Share Posted November 24, 2009 Hi Guys Are there any 'truely' random number/letter generators for PHP ? Quote Link to comment https://forums.phpfreaks.com/topic/182847-random-numletter-generators/ Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 Define "truely" random. Computers in general are incapable of "true" randomness Quote Link to comment https://forums.phpfreaks.com/topic/182847-random-numletter-generators/#findComment-965100 Share on other sites More sharing options...
thebadbad Posted November 24, 2009 Share Posted November 24, 2009 http://random.org/ offers true random numbers (via atmospheric noise). Here's a direct link to their generator: http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new (change parameters to your needs). But in most cases the PHP function mt_rand() would do the job fine I'm sure (although it's pseudorandom). Quote Link to comment https://forums.phpfreaks.com/topic/182847-random-numletter-generators/#findComment-965107 Share on other sites More sharing options...
ShaolinF Posted November 24, 2009 Author Share Posted November 24, 2009 Thanks for the replies Well, I want to generate 10 digit random strings for my password salt. Will something like rand() suffice or should I go for something which produces more unique numbers (like the one on random.org) ? Quote Link to comment https://forums.phpfreaks.com/topic/182847-random-numletter-generators/#findComment-965109 Share on other sites More sharing options...
Alex Posted November 24, 2009 Share Posted November 24, 2009 rand() is fine. Quote Link to comment https://forums.phpfreaks.com/topic/182847-random-numletter-generators/#findComment-965111 Share on other sites More sharing options...
salathe Posted November 24, 2009 Share Posted November 24, 2009 Well, I want to generate 10 digit random strings for my password salt. It wouldn't take much to cook up (or find) a function to do this; you can then define the values which are acceptable in the resultant string (numbers, upper and/or lower-case letters, other symbols, etc.). For something like this the rand and mt_rand functions are "random enough". Quote Link to comment https://forums.phpfreaks.com/topic/182847-random-numletter-generators/#findComment-965117 Share on other sites More sharing options...
bundyxc Posted November 25, 2009 Share Posted November 25, 2009 Check out the notes at rand if you're looking for a function to do it for you. A couple hours ago I added the genRandStr() function to php.net as a note, but I'll post the function/documentation here for you, while they update their mirrors. To make a ten digit string, you'd just use: genRandStr(10,10,1,1,1) Manipulating the last three 1's to zeroes will change if it uses lowercase, uppercase, and numbers (respectively). Right now, the above function will create a single ten character string, using uppercase, lowercase, and numbers. genRandStr() creates a random string, with parameters to control length, type of characters, and also the ability to create an array of these randomized strings with the established properties (as opposed to running the function multiple times, which would require program to loop through the logic portion of the function again and again). <?php function genRandStr($minLen, $maxLen, $alphaLower = 1, $alphaUpper = 1, $num = 1, $batch = 1) { $alphaLowerArray = 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'); $alphaUpperArray = 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'); $numArray = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); if (isset($minLen) && isset($maxLen)) { if ($minLen == $maxLen) { $strLen = $minLen; } else { $strLen = rand($minLen, $maxLen); } $merged = array_merge($alphaLowerArray, $alphaUpperArray, $numArray); if ($alphaLower == 1 && $alphaUpper == 1 && $num == 1) { $finalArray = array_merge($alphaLowerArray, $alphaUpperArray, $numArray); } elseif ($alphaLower == 1 && $alphaUpper == 1 && $num == 0) { $finalArray = array_merge($alphaLowerArray, $alphaUpperArray); } elseif ($alphaLower == 1 && $alphaUpper == 0 && $num == 1) { $finalArray = array_merge($alphaLowerArray, $numArray); } elseif ($alphaLower == 0 && $alphaUpper == 1 && $num == 1) { $finalArray = array_merge($alphaUpperArray, $numArray); } elseif ($alphaLower == 1 && $alphaUpper == 0 && $num == 0) { $finalArray = $alphaLowerArray; } elseif ($alphaLower == 0 && $alphaUpper == 1 && $num == 0) { $finalArray = $alphaUpperArray; } elseif ($alphaLower == 0 && $alphaUpper == 0 && $num == 1) { $finalArray = $numArray; } else { return FALSE; } $count = count($finalArray); if ($batch == 1) { $str = ''; $i = 1; while ($i <= $strLen) { $rand = rand(0, $count); $newChar = $finalArray[$rand]; $str .= $newChar; $i++; } $result = $str; } else { $j = 1; $result = array(); while ($j <= $batch) { $str = ''; $i = 1; while ($i <= $strLen) { $rand = rand(0, $count); $newChar = $finalArray[$rand]; $str .= $newChar; $i++; } $result[] = $str; $j++; } } return $result; } } print_r(genRandStr(5, 10, 1, 0, 1, 1)); //Output is a single string 5-10 characters long, using solely lowercase and numbers. print('<br /><br />'); print_r(genRandStr(8, 8, 0, 1, 0, 50)); //Output is an array containing 50 strings that are only uppercase, and 8 characters long. print('<br /><br />'); print_r(genRandStr(5, 10)); //Output is a single string 5-10 characters long, using the full alphabet (uppercase and lowercase), and all numbers. /* genRandStr($minLen, $maxLen, $alphaLower = 1, $alphaUpper = 1, $num = 1, $batch = 1) $minLen is the minimum length of the string. Required. $maxLen is the maximum length of the string. Required. $alphaLower toggles the use of lowercase letters (a-z). Default is 1 (lowecase letters may be used). $alphaUpper toggles the use of uppercase letters (A-Z). Default is 1 (uppercase letters may be used). $num toggles the use of numbers (0-9). Default is 1 (numbers may be used). $batch specifies the number of strings to create. Default is 1 (returns one string). When $batch is not 1 the function returns an array of multiple strings. */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/182847-random-numletter-generators/#findComment-965220 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.