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

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

    First: If the number is guaranteed to never repeat, it's not very random.

    Second: There are lots of PRNG algorithms.

    UPDATE:

    Third: There's an IETF RFC for UUIDs (what MS calls GUIDs), but you should recognize that (U|G)UIDs are not cryptographically secure, if that is a concern for you.

    UPDATE 2:

    If you want to actually use something like this in production code (not just for your own edification) please use a pre-existing library. This is the sort of code that is almost guaranteed to have subtle bugs in it if you've never done it before (or even if you have).

    UPDATE 3:

    Here's the docs for .NET's GUID

提交回复
热议问题