Java: Randomly generate distinct names

前端 未结 8 722
臣服心动
臣服心动 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: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..

提交回复
热议问题