views:

240

answers:

9

I have two functions, makeKey() and keyExists().

makeKey() simply generates a 5 digit random alphanumeric key, keyExists() accepts this key as its only argument, and looks up in a table, returning true/false depending on whether it exists.

I need to do something very simple but I cannot figure out the quickest way of doing it.

I just need to make a key, and if it exists in the table, make a key again, and so on until a unique one is returned. I think a while loop will suffice?

Thanks and please forgive the rather basic question, I think I cooked my brain in the sun yesterday.

+2  A: 

my php is a little rusty, so consider this pseudo-code:

$key_exists = true;
while($key_exists) {
       $key = generateKey();
       $key_exists = checkKey($myKeysHash, $key);
}
// $key is now unique and ready to use
Tim Hoolihan
+4  A: 

I’d use a do-while loop:

do {
    $newKey = makeKey();
} while (keyExists($newKey));

This will generate a new key on every iteration until the key does not exist yet.

Gumbo
A: 

You mention a table, so I'm wondering if you are storing these keys in a database? If so, your approach is going to have a race condition - you might check a key is OK to use right before another process uses that key.

A better approach is generate a possible key and then attempt to persist it - perhaps by performing an INSERT onto a table of keys and retrying with different keys until it succeeds.

Paul Dixon
+5  A: 

Any solution that relies on creating, then checking is going to have awful performance as the key space fills up. You'd be better off generating a unique key using an autogenerated column (identity or guid). If it needs to be alphanumeric, use a mapping function to transform it into the alphabet of your choice by selecting groups of bits and using them as an index into your alphabet.

Pseudo-code

alphabet = "ABCDE...789";
key = insert new row, get autogenerated key
alphaKey = "";
while (get n bits from key)
   alphaKey += alphabet[bits]
done
echo alphaKey
tvanfosson
A: 

if your not fixed on a 5 digit number, you could think about using a hash of your id + a name column

solomongaby
+1  A: 

Why not use a built in php function like uniqid()?

Matt
A: 

I'll also assume you're using some sort of database.

Could you not use a unique auto-increment ID column in the database? It would remove the requirement to check if the key exists since the database engine will never assign the same ID twice.

However, you'd have to change the logic in your application rather than just coding up new functions.

T Pops
A: 

Does it need to be random? Just increment a variable and store the next one to be used in another field.

Cybergibbons
A: 

while (keyExists($newKey = makeKey()));

Probably the quickest way of doing the check, if a key exists it will generate a new one. If you start having a lot of collisions/needing to check the database many times before getting a new unique key, you probably will want to rethink your makeKey() algorithm. Calls to the DB are expensive, the fewer calls you can make the faster and more efficient your script will be.

DashRantic