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

后端 未结 6 1417
忘了有多久
忘了有多久 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:19

    I understand that you are seeking a way to generate random number using C#. If yes, RNGCryptoServiceProvider is what you are looking for.

    [EDIT]

    If you generate a fairly long number of bytes using RNGCryptoServiceProvider, it is likely to be unique but there is no gurantee. In theory, true random numbers doesnt mean to be unique. You roll a dice 2 times and you may get head both the times but they are still random. TRUE RANDOM!

    I guess to apply the check of being unique, you just have to roll out your own mechanism of keeping history of previously generated numbers.

提交回复
热议问题