Jump to content

generate unique strings of characters


needs_upgrade
Go to solution Solved by lemmin,

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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.

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.