How to generate “random” but also “unique” numbers?

后端 未结 6 1520
忘了有多久
忘了有多久 2021-02-05 21:32

How are random numbers generated.? How do languages such as java etc generate random numbers, especially how it is done for GUIDs.? i found that algorithms like Pseudorandomnumb

6条回答
  •  天命终不由人
    2021-02-05 22:22

    Specifically regarding Java:

    • java.util.Random uses a linear congruential generator, which is not very good
    • java.util.UUID#randomUUID() uses java.security.SecureRandom, an interface for a variety of cryptographically secure RNGs - the default is based on SHA-1, I believe.
    • UUIDs/GUIDs are not necessarily random
    • It's easy to find implementations of RNGs on the net that are much better than java.util.Random, such as the Mersenne Twister or multiply-with-carry

提交回复
热议问题