Is there a way to generate a random UUID, which consists only of numbers?

后端 未结 10 1517
一生所求
一生所求 2020-12-09 09:27

Java\'s UUID class generates a random UUID. But this consists of letters and numbers. For some applications we need only numbers. Is there a way to generate random UUID that

相关标签:
10条回答
  • 2020-12-09 09:47

    Although the original poster of this question marked @Jigar Joshi's question as the best answer, I'd like to add that there is a rationale to use UUIDs rather than random numbers in certain cases. The difference is that UUIDs are (universally) unique (hence the name) while random numbers are not. It's just a question of probability whether you get the same number twice or even more often when generating random numbers, but this will never happen with UUIDs. Therefore, I'd consider @Chris Eibl's answer as the best one.

    0 讨论(0)
  • 2020-12-09 09:50

    Call hashCode() on the UUID to get a unique integer.

    0 讨论(0)
  • 2020-12-09 09:56

    Why using the UUID class if you dont need an UUID? It sounds more like you need just a random number which can be achieved by using the java.util.Random class.

    0 讨论(0)
  • 2020-12-09 09:58

    String uuid = UUID.randomUUID().toString().replaceAll("-", ""); //strips '-'

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