Glese Posted November 29, 2011 Share Posted November 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/ Share on other sites More sharing options...
The Little Guy Posted November 29, 2011 Share Posted November 29, 2011 the easiest way to generate a unique number is to use a myql auto_increment. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292053 Share on other sites More sharing options...
Psycho Posted November 29, 2011 Share Posted November 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292070 Share on other sites More sharing options...
Glese Posted November 29, 2011 Author Share Posted November 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292100 Share on other sites More sharing options...
trq Posted November 29, 2011 Share Posted November 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292107 Share on other sites More sharing options...
Glese Posted November 29, 2011 Author Share Posted November 29, 2011 In my initial post I stated I want to use the random id for the title of the contribution. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292114 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2011 Share Posted November 29, 2011 Then it doesn't matter if it's duplicated, as long as you aren't using it as the primary key in the DB table. Why would you want to use a non-descriptive title for an article anyhow? Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292169 Share on other sites More sharing options...
Glese Posted November 29, 2011 Author Share Posted November 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292170 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2011 Share Posted November 29, 2011 Regardless of whether it's an article or contribution, the operative word here is 'title'. Titles are meant to be descriptive. And it still doesn't matter if the 'title' is duplicated as long as it isn't a DB key. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292172 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2011 Share Posted November 29, 2011 Regarding the uniqid() function, it doesn't use only the value of the microseconds from the current time, it uses the entire timestamp with milliseconds. Once the current millisecond has passed, it will never be available again. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292174 Share on other sites More sharing options...
Glese Posted November 29, 2011 Author Share Posted November 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292177 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2011 Share Posted November 29, 2011 The current time refers to the current unix timestamp, which is the total number of elapsed seconds since 1970-01-01 00:00:00 GMT. uniqid() further expands that value to include the milliseconds. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292179 Share on other sites More sharing options...
Glese Posted November 29, 2011 Author Share Posted November 29, 2011 Ohh I misread it, it is saying IN microseconds, and not just the microseconds. No surprise, if it was not like this, unique id, would not be an unique id, this way it is authentically an unique id. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292191 Share on other sites More sharing options...
Psycho Posted November 29, 2011 Share Posted November 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292319 Share on other sites More sharing options...
xyph Posted November 29, 2011 Share Posted November 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292323 Share on other sites More sharing options...
The Little Guy Posted November 29, 2011 Share Posted November 29, 2011 With an md5, you have: 63,340,286,662,973,277,706,162,286,946,812,000,000,000,000,000,000 different combinations. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292329 Share on other sites More sharing options...
xyph Posted November 29, 2011 Share Posted November 29, 2011 Well assuming you're using an auto increment column to generate the hash, the chances of a collision are trivial. If you then append the integer used to make this hash, a duplicate becomes impossible. Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292331 Share on other sites More sharing options...
shlumph Posted November 29, 2011 Share Posted November 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292338 Share on other sites More sharing options...
xyph Posted November 29, 2011 Share Posted November 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292341 Share on other sites More sharing options...
ignace Posted November 29, 2011 Share Posted November 29, 2011 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. Programming is pretty industrial and lifeless. You can always throw some art it's way: http://acme.inc/this-is-not-a-pk/1 Quote Link to comment https://forums.phpfreaks.com/topic/252002-which-is-the-most-efficient-way-to-generate-an-unique-random-number/#findComment-1292350 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.