php-windows Digest 10 Jun 2005 02:35:57 -0000 Issue 2690
Topics (messages 26085 through 26087):
how to avoid repetition?
26085 by: bo
26086 by: Murray . PlanetThoughtful
26087 by: graeme
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
I need to generate 10million 12-char string and I am able to do so, however
the problem is how to avoid repetition keeping each string unique? it will
be a disaster to compare each string,isn't it?
ps Thanks Yong.
Bo
--- End Message ---
--- Begin Message ---
> I need to generate 10million 12-char string and I am able to do so,
> however
> the problem is how to avoid repetition keeping each string unique? it will
> be a disaster to compare each string,isn't it?
One way to handle this would be to insert the 10 million values into a MySQL
table [1] that has one varchar(12) field with no indexes.
Then, once the records are in the table, issue the following sql statement:
ALTER IGNORE TABLE lotsofids ADD UNIQUE INDEX(random_id)
This will effectively remove any duplicate entries from the table, and then
a:
SELECT COUNT(*) FROM lotsofids
... would tell you if you need to generate any more ids to 'top up' the
table to 10 million. It will also give you some idea of how effective the
randomization process you're using to generate the values is.
Assumptions made above:
- Your table name is 'lotsofids' and your field name is 'random_id'.
Obviously, you can change these to whatever you prefer.
Regards,
Murray
[1] Assuming you have MySQL available -- a google on whichever db server you
have using keywords "remove duplicates <insert name of db server here>"
should bring up similar methods
--- End Message ---
--- Begin Message ---
A couple of ideas to improve the chances of generating unique strings
You could create 20,000,001 char(6) strings, merge the first with the
last, second with the second last and so on, creating char(12) strings
or
You could create 20,000,001 char(7) strings, merge the first with the
last, second with the second last and so on, creating char(14) strings
and then dropping two digits at random.
BUT if you must guarantee that the strings are unique then you will need
to do a comparison of the resulting strings. Using a database to sort
and then compare is as good an approach as any, since database have good
sorting algorithms and are designed to manage large numbers.
graeme.
bo wrote:
I need to generate 10million 12-char string and I am able to do so, however
the problem is how to avoid repetition keeping each string unique? it will
be a disaster to compare each string,isn't it?
ps Thanks Yong.
Bo
--
Experience is a good teacher, but she sends in terrific bills.
Minna Antrim
--- End Message ---