needs_upgrade Posted April 16, 2013 Share Posted April 16, 2013 Hello guys. How can i make 50,000 unique string characters? What i have in mind is to md5() numbers 1-50,000. But my client wants those strings to be 10 characters in length only. Where should i start? Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/277005-generate-unique-strings-of-characters/ Share on other sites More sharing options...
NardCake Posted April 16, 2013 Share Posted April 16, 2013 Well I'm not quite sure if my way is a guaruntee but it makes sense to me. Simply I take the alphabet, lower and upper case, numbers 1-10 and the current timestamp and mix them all together. <?php $chars = str_shuffle('abcdefghijklmnopqrstuvwxyz1234'.time().'567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'); $chars = substr($chars,0,10); echo $chars; ?> I'm not anywhere with the ability to test that on a server at the moment, but hopefully that does something near to what you want? Then of course for the loop if you wanted to generate the strings for a database for example: <?php $count = 0; while($count <= 50000){ // enter code above then enter into database or something $count++; } ?> I apologize for any errors. Hopefully that does something similar to what you want. Quote Link to comment https://forums.phpfreaks.com/topic/277005-generate-unique-strings-of-characters/#findComment-1425039 Share on other sites More sharing options...
davidannis Posted April 16, 2013 Share Posted April 16, 2013 You could do the md5 thing and just take a substring, but I'd look at modifying this if you want really random strings. Quote Link to comment https://forums.phpfreaks.com/topic/277005-generate-unique-strings-of-characters/#findComment-1425054 Share on other sites More sharing options...
Q695 Posted April 16, 2013 Share Posted April 16, 2013 Try doing this: <?php $i = 1; while ($i <= 50000) { echo(rand(1000000000,9999999999)); $i++; /* the printed value would be $i before the increment (post-increment) */ } then doing a database comparison, and then loading it into the database, if the database doesn't already have the number implemented Quote Link to comment https://forums.phpfreaks.com/topic/277005-generate-unique-strings-of-characters/#findComment-1425125 Share on other sites More sharing options...
mac_gyver Posted April 16, 2013 Share Posted April 16, 2013 you would want to store random generated values in an array, use array_unique to remove duplicates and keep generating them until you have 50000 unique values or just generate a larger incrementing series, two to three times the needed number of values, shuffle the result and use the first 50000 of them. trying to insert the values into the database as they are generated, ignoring duplicates using a unique key and keeping a count of how many values have been inserted would take a very long time to run using a query inside of a loop. once you have the 50000 unique values in an array, insert them as many at a time as you can using a multivalue insert query. Quote Link to comment https://forums.phpfreaks.com/topic/277005-generate-unique-strings-of-characters/#findComment-1425134 Share on other sites More sharing options...
Solution lemmin Posted April 16, 2013 Solution Share Posted April 16, 2013 I think davidannis has the right idea. If this is a one-time population for a database, the chances of you hitting two identical md5s (even truncated) is extremely low. I just tested a sample and I can't even get one duplicate. $array = array(); for($i=0;$i<50000;$i++) { $array[] = substr(md5(microtime(1)+$i), 0, 10); } $unique = array_unique($array); echo sizeof($array).'<br/>'; echo sizeof($unique).'<br/>'; You could even implement NardCake's idea into this if you're paranoid: $array[] = str_shuffle(substr(md5(microtime(1)+$i), 0, 10)); I had to go up to 1,000,000 at a time before I hit a duplicate consistently (maybe 50% of the time). But, like I said, at 50,000, you will be lucky to get a duplicate. I guess I'm saying that the best method depends on the purpose of the data. Quote Link to comment https://forums.phpfreaks.com/topic/277005-generate-unique-strings-of-characters/#findComment-1425184 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.