23

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.

well actually
  • 11,810
  • 19
  • 52
  • 70

8 Answers8

81

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

Gaurav Lad
  • 1,788
  • 1
  • 16
  • 30
21
// 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();
}
corsiKa
  • 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
  • 6
    No, 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
17

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
Rany Albeg Wein
  • 3,304
  • 3
  • 16
  • 26
Rami C
  • 1,903
  • 1
  • 12
  • 14
2

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.

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
2

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[]{});
}
Bladean Mericle
  • 524
  • 2
  • 13
1

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

Sergey Vedernikov
  • 7,609
  • 2
  • 25
  • 27
1

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..

evergreen
  • 736
  • 1
  • 9
  • 17
1

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130