Java: Randomly generate distinct names

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

    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.

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

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

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