Java: Randomly generate distinct names

2019-03-19 14:04发布

问题:

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.

回答1:

// 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();
}


回答2:

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



回答3:

Why not use java.util.UUID? It is guaranteed to generate unique identifiers, and it is as standard as it gets :-).



回答4:

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[]{});
}


回答5:

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



回答6:

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



回答7:

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.



回答8:

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.