I need to generate 10,000 unique identifiers in Java. The identifiers should be a mixture of numbers and letters and less than 10 characters each. Any ideas? Built in libraries would be an extra plus.
8 Answers
I am answering this very late, but this is what really useful for new reader. This is a very simple and efficient way to get random VALID names. To do so, add maven repository in POM.xml
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.12</version>
</dependency>
And then use the Faker class as below in your Java code
Faker faker = new Faker();
String name = faker.name().fullName();
String firstName = faker.name().firstName();
String lastName = faker.name().lastName();
String streetAddress = faker.address().streetAddress();
Try printing the result using standard System.out.println();
For more reference Faker Lib

- 1,788
- 1
- 16
- 30
-
Uniqueness is not guaranteed, as requested by OP. – html_programmer Dec 28 '21 at 07:26
-
The problem is the names don't have some `gender` property. I can't have these random names match a specific gender so you may end up with some John that is a female – JoreJoh Aug 17 '23 at 21:39
// class variable
final String lexicon = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890";
final java.util.Random rand = new java.util.Random();
// consider using a Map<String,Boolean> to say whether the identifier is being used or not
final Set<String> identifiers = new HashSet<String>();
public String randomIdentifier() {
StringBuilder builder = new StringBuilder();
while(builder.toString().length() == 0) {
int length = rand.nextInt(5)+5;
for(int i = 0; i < length; i++) {
builder.append(lexicon.charAt(rand.nextInt(lexicon.length())));
}
if(identifiers.contains(builder.toString())) {
builder = new StringBuilder();
}
}
return builder.toString();
}

- 81,495
- 25
- 153
- 204
-
Why aren't the class variable private? Do they really need to be package scope? – Steve Kuo Feb 17 '11 at 07:15
-
6No, they really don't. The example above is for algorithmic purposes only. I would hope anyone who uses this would not copy and paste this, but instead would apply the concepts illustrated to their solution. – corsiKa Feb 17 '11 at 07:19
-
There's a ) missing in "if(identifiers.contains(builder.toString())" and the program is syntactically wrong. Would correct it myself, but edits require at least 6 chars of modification :) – MSX Oct 04 '16 at 09:06
-
@MSX thanks - next time I would say just make some other minor changes and make it clear what you were fixing. =) Wish I could give you the rep points for the edit... :( – corsiKa Oct 04 '16 at 19:30
Why not use java.util.UUID
?
It is guaranteed to generate unique identifiers, and it is as standard as it gets :-).
e.g.
String random = UUID.randomUUID().toString();
Or even
int desiredLength = 5;
String random = UUID.randomUUID()
.toString()
.substring(0, desiredLength);
Which will result in some random String
of desiredLength
, like:
6e9c3

- 3,304
- 3
- 16
- 26

- 1,903
- 1
- 12
- 14
I had the same problem, but I needed an arbitrarily long string. I came up with this one-liner, no external library needed, that will give you 10 characters:
BigInteger.probablePrime(50, new Random()).toString(Character.MAX_RADIX)
The length can be changed, you need about 5 bits per character. What did is filter and limit the length as follows (just lowercase letters, and size 10):
BigInteger.probablePrime(100, new Random()).
toString(Character.MAX_RADIX).
replaceAll("[0-9]", "").
substring(0, 10)
Disadvantage: it's a bit slow.

- 48,905
- 14
- 116
- 132
If you permit Apache Commons lang...
public String[] getRandomlyNames(final int characterLength, final int generateSize) {
HashSet<String> list = new HashSet<String>();
for (int i = 0; i < generateSize; ++i) {
String name = null;
do {
name = org.apache.commons.lang.RandomStringUtils.randomAlphanumeric(
org.apache.commons.lang.math.RandomUtils.nextInt(characterLength - 1) + 1);
while(list.contains(name));
list.add(name);
}
return list.toArray(new String[]{});
}

- 524
- 2
- 13
You can try to take md5 hash of current time and you will get "random" identifier as mixture of numbers and letters

- 7,609
- 2
- 25
- 27
-
And what happens when you do it twice in the same time interval (nanos and even millis don't update all that fast...) – corsiKa Feb 17 '11 at 06:20
-
1or just iteration over 10000 numbers from random initial number – Sergey Vedernikov Feb 17 '11 at 06:21
-
that's actually really neat. Probably does a bit more calculation than you need, but it would just be a couple lines of code. – corsiKa Feb 17 '11 at 06:23
-
`Random random = new Random(); for (int i=0; i<10000; i++) { System.out.println(Long.toHexString(random.nextLong())); }` – Vance Maverick Feb 17 '11 at 06:43
The easiest and fastest way is to generate permutations of a certain string. As long as the string is long enough, you can easily have 10,000 unique permutations. The good thing of generating permutation is that you don't have to worry about duplications. If a string contains all different characters, it can generate n! permutations (n is the length of the string). So a string with 8 different characters can generate 40,320 different permutations.
There are many code on-line to generate permutations of a string, such as this one http://introcs.cs.princeton.edu/23recursion/Permutations.java.html.
If you want them to be more random, you can use different strings as the seed, such as "abcde123", "efgh456", etc..

- 736
- 1
- 9
- 17
You could try
Random rand = new Random();
Set<String> words = new HashSet<String>();
while(words.size() < 10000)
words.add(Long.toString(Math.abs(rand.nextLong() % 3656158440062976L), 36)));
The long constant is just enough for 10 digit, base 36 numbers.

- 525,659
- 79
- 751
- 1,130