Generating a random key

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Generating a random key

Post by JellyFish »

I was wondering if there was already of php function included with php that can accomplish generating a random string of 32 numbers, upper-case letters, and lower-case letters? For example, "1a2b3d4f5s6D7f8s9S0s1G3j48DgaIb8".

If there isn't a function included in php main library, then how would I go about creating such a function. What would be the best way to generate a string like this? What I mean by best, the most clean code-wise, uses the least amount of server resources, etc.

I have an idea of how I could do this. I don't want to stop you from coming up with your own and potentially better solution so don't mind to much with my idea if you think you have a better solution.

Sense the string is 32 characters long, and sense each character can hold 0-9-a-z-A-Z then that means each character is 62 bits (0 to 9 + a to z + A to Z = 10 + 26 + 26 = 62). Sense each character is 62 bits that means that the whole string is 62^32 bits (32 because that's the length of the string). This is obviously a large number so large that you need to use the e+ notation to express it:

2.2726578844967515e+57

So, my idea is generate a random number from 0 to 62^32 and then convert that to base 62! :D Sense my string is virtually just a base 62 number with 32 digits, this would work, right?

The only reason I haven't done this yet is because I don't know how to generate a random number in the range of 0 to 62^32 and then convert that to base 62?

Thanks for reading. All help is very, very so much appreciated. :lol:
Last edited by JellyFish on Sat Mar 07, 2009 4:03 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Generating a random key

Post by Benjamin »

You are free to use this:

viewtopic.php?f=50&t=96344
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Generating a random key

Post by JellyFish »

I think I got it. Writing the OP helped me to think it though a bit.

Code: Select all

   function makeKey()
    {
        $chars = str_split("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        
        for ($i = 0; $i < 32; $i++)
        {
            $randNum = rand(0, 61);
            $key .= $chars[$randNum];
        }
        return $key;
    } 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Generating a random key

Post by requinix »

You don't even need the str_split: you can use [] with strings too.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Generating a random key

Post by JellyFish »

tasairis wrote:You don't even need the str_split: you can use [] with strings too.
What do you mean?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Generating a random key

Post by requinix »

JellyFish wrote:
tasairis wrote:You don't even need the str_split: you can use [] with strings too.
What do you mean?

Code: Select all

$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$chars[0] is "0", $chars[10] is "a", and $chars[61] is "Z".
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Generating a random key

Post by JellyFish »

tasairis wrote:
JellyFish wrote:
tasairis wrote:You don't even need the str_split: you can use [] with strings too.
What do you mean?

Code: Select all

$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$chars[0] is "0", $chars[10] is "a", and $chars[61] is "Z".
Oh sweet! I never knew that until now. Thanks. :D
Post Reply