Jump to content

random num/letter generators


ShaolinF

Recommended Posts

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).
Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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.

*/
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.