Generate a random ID





27
Date Submitted Fri. Sep. 22nd, 2006 7:06 AM
Revision 1 of 1
Helper ffxfiend
Tags "Random Generation" | PHP | phpcode
Comments 5 comments
This is a simple function to generate a random ID of letters and numbers however long you want. All you do is pass it how many parts you want and then how many pieces(chars/numbers) per part you want.

I hope you enjoy


/** FUNCTION: generate_ID
*
* This function generates a random ID of letters and numbers
* as long as is desired.
*
* @ARGUMENTS: 2
*                         - $np: This determines how many parts are randomly
*                                   generated. Some parts will be numbers and
*                                   some will be letters.
*                         - $npp: This is the number of pieces-per-part. Each
*                                   part is made up of multiple pieces
* @RETURNS:
*                         - $id: The randomly Generated ID
**/

function generate_ID($np,$npp)
{
        // The id to create
        $id = "";
       
        // Create an array of characters
        $aplha = "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";
        $aplha = explode(" ", $aplha);
       
        for ($j = 0; $j < $np; $j++)
        {
                $r = rand(1,2);
                for ($i = 0; $i < $npp; $i++)
                {
                        if ($r == 1)
                        {
                                // Generate random numbers
                                $id .= rand(1,9);
                        }
                        else
                        {
                                // Generate random letters
                                $a = rand(1,26);
                                $id .= $aplha[$a];
                        }
                }
        }
        return $id;
}

 

Jeremiah Poisson

www.igzactly.com
*** Stay sane inside insanity ***

Comments

Comments Interesting
Mon. Oct. 2nd, 2006 9:47 AM    Scripter ctiggerf
Comments Complicated
Fri. Jan. 19th, 2007 3:21 PM    Helper inxilpro
  Comments Not quite
Sat. May. 12th, 2007 10:53 AM    Helper albud
Comments useful
Mon. Oct. 16th, 2006 4:46 PM    Beginner lardissone
Comments Nice
Fri. Oct. 20th, 2006 3:11 PM    Beginner HardPLay

Voting