Jump to content

Which is the Most Efficient Way to Generate an Unique Random Number?


Glese

Recommended Posts

I am looking to generate a random number for every user contribution as a title of the contribution.

 

I could simply check the database each time with a query and generate a number which does not equal to any of the entries of the database. But I imagine this as inefficient and it could become slow if the database is big in my opinion. Also I'd have to contain all the numbers of the database somewhere to manage the "not equals to", in an array or something similar but that can end up as a giant one.

 

Excuse the layman's speech I am new to this.

 

Any suggestions how this can be solved efficiently without straining the resources too much? You can explain it linguistically and do not have to provide me any scripts, I will figure it out.

Link to comment
Share on other sites

the easiest way to generate a unique number is to use a myql auto_increment.

 

Which is not random, but why do you need it to be random? Using an auto-increment column is the best way to ensure each record has a unique primary key. You should have a really good reason for needing a random unique number. The problem is that the more records you have, the larger you need to make the pool of possible numbers to ensure you don't get caught in a loop looking for an available value and the complexity will grow.

Link to comment
Share on other sites

I just think if it is done with auto increment the page will look a bit industrial and lifeless, with random numbers it gives it more life.

 

What do you think of using uniqid(), I just tried it and it does work, I am just wondering if the ID is really always unique or if two can happen coincidentally as well.

 

Other than that I could start a number at 10000 and do it with auto increment from there on.

Link to comment
Share on other sites

I just think if it is done with auto increment the page will look a bit industrial and lifeless, with random numbers it gives it more life.

 

Sorry, but this is just funny. Why would what a primary key looks like be of any concern? It is there to aid in database normalisation, not to look cool.

Link to comment
Share on other sites

I did not say it is an article, I said it is a contribution, I am working on a game like application for teenagers, I do not want to go in further detail.

 

Though notice, the documentation states that the uniqid function does use the milli seconds to generate, in this sense a duplicate ID is technically possible, yet the possibility is very low that it  hits the exact same mili seconds twice. A modification can make it even less likely possible by appending the user_id for example as a prefix.

Link to comment
Share on other sites

Excuse me, I am a new comer, maybe I am missing something, but this is what the manual states:

 

 

Gets a prefixed unique identifier based on the current time in microseconds.

 

 

It is not milliseconds, it is microseconds, calling it milliseconds was my mistake, sorry about that.

 

Though in the whole document on that page it does not state anything about the whole timestamp, as said maybe I am missing something, you saw?

 

http://de2.php.net/manual/en/function.uniqid.php

Link to comment
Share on other sites

OK, let's back up a second. You want some random number to use for the title of the contribution. Whether you do that or not, you should still use an auto-incrementing field for the records primary id. Using that, you can have the primary ID field as a relatively smaller int type than you would need for a random value. Which should result in better performance.

 

So, as Pikachu2000 stated, it really doesn't matter if you do get a duplicate. The chances, as you already said, would be very small. No one is going to see some randomly generated title for a contribution and say "Hey I've seen that number used on a contribution from four months ago". But, since this value really would have no functional purpose, you have many options. You could do an MD5() or SHA() hash of the timestamp + userID, for example.

Link to comment
Share on other sites

I'm assuming he/she will be using the 'title' in the URI

 

mysite.com/contribs/q3464fdbcbbx345234/

 

Another solution is to just use an auto-increment field, and hash it to make it seem more random. You can then append the ID using a delimiter that wouldn't be found in the hash result.

 

<?php

$id = 5;
$delimiter = 'z'; // MD5 is hex, and will only output 0-9,a-f

$display_id = substr( md5($id),0,10 ) . // Limit the md5 output to only 10 characters
$delimiter . $id; // Attach the delimiter/ID at the end so it can be extracted easily

echo 'Display ID: ' . $display_id . '<br>';

echo 'Original ID: ' . substr( $display_id, strpos($display_id,$delimiter)+1 );

?>

 

This will always be unique, look 'cool,' be very quick, and stored/referenced easily.

Link to comment
Share on other sites

Just use auto_increment and then wherever the number is displayed, use base64_encode to make it look "cool".  If you need to extract the "cool" number to do a DB lookup, use base64_decode

 

base64 can return characters that are not URI-friendly (plus and slash). Be sure to URL-encode the values.

http://php.net/manual/en/function.base64-encode.php#63543

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.