The Little Guy Posted January 8, 2010 Share Posted January 8, 2010 What would be the best way to create a unique and random 10 digit number that isn't in a mysql database, without querying the database many times? I know 10 is a large enough number so it probably will be hard to have to query the db more than once to see if the number already exists, but I believe that means I can have: 10,000,000,000 unique numbers but say 98% of those are taken, which means there are 980,000,000 numbers left. Which means I would have a greater chance of having to query the database more that once or twice. How can I minimize the number of times I have to query the database? I would also like to be able to do this with a 32 alpha numeric string, and one that is about 50 alpha numeric characters (with dashes). Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/187665-unique-ids/ Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 You could use an auto increment for your first id problem, that would guarantee each is unique As for the second, use a random generator. I have an example on my website http://www.jaygilford.com/php/php-random-string-generator-function/ Quote Link to comment https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990755 Share on other sites More sharing options...
laffin Posted January 8, 2010 Share Posted January 8, 2010 I would set up a bitstring. but 10,000,000,000 is quite a large bitstring. so u may just opt for a bitmask file Quote Link to comment https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990757 Share on other sites More sharing options...
crabfinger Posted January 8, 2010 Share Posted January 8, 2010 Well in my opinion you can never have a "randomly" generated number that won't in some way have the chance of being duplicated over a large period of time. Why don't you just have a function to say make an md5 of a unique string, depending on what you're doing this could be a user name or file name or whatever, then replace all of the letters with their corresponding numbers, then substr the first 10? Quote Link to comment https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990763 Share on other sites More sharing options...
The Little Guy Posted January 8, 2010 Author Share Posted January 8, 2010 ... then replace all of the letters with their corresponding numbers... How do you replace letters with corresponding numbers? Or should I say how do you know what the corresponding number is to a letter? Quote Link to comment https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990765 Share on other sites More sharing options...
laffin Posted January 8, 2010 Share Posted January 8, 2010 Actually I did think of one more way.... if you create all accounts beforehand u can have a status field, where ya can set it as in use or not in use with a query, you can retirve how many accts are not in use with this, you can generate a number between 1 and not in use accts with another query u can fetch the id dummy code $arr=mysql_fetch_row(mysql_query("SELECT Count(id) FROM users WHERE status='not in use')); $not_in_user=$arr[0]; $newid=rand(1,$not_in_use); $arr=mysql_fetch_assoc(mysql_query('SELECT * FROM users WHERE status='not in use' AND LIMIT=1 OFFSET=$newid")); $id=$arr['id'] That would be the simplest, but as said, all accounts must be already in the database.... Hopefully above dummy code is some help Quote Link to comment https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990776 Share on other sites More sharing options...
The Little Guy Posted January 8, 2010 Author Share Posted January 8, 2010 I came up with this function: Any thoughts? function randomID($len = 10){ $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $tid = md5(uniqid().time()); $len = strlen($tid); $id = ''; $tmp = ''; for($i = 0;$i < $len; $i++){ $rand = round(rand(0, 1)); $pos = strpos($str, $tid{$i}); if($rand == 0 && ctype_alpha($tid{$i})){ $tmp .= $str{$pos + 26}; }else{ $tmp .= $tid{$i}; } } for($i = 0;$i < $len; $i++){ if(!ctype_digit($tid{$i})){ $id .= strpos($str, $tid{$i}); }else{ $id .= $tid{$i}; } } return substr($id, 0, $len); } Quote Link to comment https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990777 Share on other sites More sharing options...
crabfinger Posted January 8, 2010 Share Posted January 8, 2010 ... then replace all of the letters with their corresponding numbers... How do you replace letters with corresponding numbers? Or should I say how do you know what the corresponding number is to a letter? err... $letters = range('a','z'); $numbers = range('1','26'); $string = str_ireplace($letters,$numbers,$string); Quote Link to comment https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990778 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.