Java: Randomly generate distinct names

前端 未结 8 695
臣服心动
臣服心动 2020-12-09 04:58

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 librar

相关标签:
8条回答
  • 2020-12-09 05:07

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

    0 讨论(0)
  • 2020-12-09 05:08

    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

    0 讨论(0)
  • 2020-12-09 05:13

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

    0 讨论(0)
  • 2020-12-09 05:14
    // 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();
    }
    
    0 讨论(0)
  • 2020-12-09 05:22

    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[]{});
    }
    
    0 讨论(0)
  • 2020-12-09 05:27

    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.

    0 讨论(0)
提交回复
热议问题