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

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

    There are a lot of ways you could generate random numbers. It's usually done with a system/library call which uses a pseudo-number generator with a seed as you've already described.

    But, there are other ways of getting random numbers which involve specialized hardware to get TRUE random numbers. I know of some poker sites that use this kind of hardware. It's very interesting to read how they do it.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 22:21

    You could use this code sample: http://xkcd.com/221/ Or, you can use this book: http://www.amazon.com/Million-Random-Digits-Normal-Deviates/dp/0833030477

    But seriously, don't implement it yourself, use an existing library. You can't be the first person to do this.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-05 22:33

    Most random number generators have a way to "randomly" reïnitialize the seed value. (Sometimes called randomize).

    If that's not possible, you can also use the system clock to initialize the seed.

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