Looking for id/key generator

I

Igor

Hi all,

I am looking for an ID/key generator in Java. It can be a class or a
package.
I need the result to be something like 2AF33Y88 - digits and letters
together.
It can be fixed size or I have to set the length.

Thanks in advance,
Igor
 
R

Roy Epperson

Here's a portion of a class I wrote to generate keys. I also have a method
to reject offensive words and fragments of words and a method that look into
a table of already generated keys to insure the new key is unique. Let me
know privately if you're interested in those.

private static final String characterMap =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static String[] generate(int randomStringLength, int
numberOfStrings, Random random){
String ans[] = new String[numberOfStrings];
for(int x= 0; x < numberOfStrings; x++){
StringBuffer sb = new StringBuffer();
for(int n = 0; n <randomStringLength; n++){
sb.append(characterMap.charAt(random.nextInt(61)));
}
ans[x] = sb.toString();
}
return ans;
}
 
J

Johann Burkard

S

Steve Horsley

One question.

What if you need not duplicated ids?
Is it enought if you use both timestamp + thread_id ?

Thank you.

Can you be sure the same thread won't hand out 2 nummbers within one
clock-tick (55mS on Win95)?

How about a class that hands out incrementing int values (let it wrap) and
concatenate that int value with the current time. You are unlikely to dish
out 4 billion IDs within 55mS.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top