如何生成“随机”但也是“唯一”数字?

时间:2022-11-25 11:30:53

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 Pseudorandomnumber generator uses initial values.

如何生成随机数。诸如java等语言如何生成随机数,尤其是如何为GUID完成。我发现像Pseudorandomnumber生成器这样的算法使用初始值。

But i need to create a random number program, in which a number once occurred should never repeats even if the system is restarted etc. I thought that i need to store the values anywhere so that i can check if the number repeats or not, but it will be too complex when the list goes beyond limits.?

但我需要创建一个随机数程序,其中一次发生的数字应该永远不会重复,即使系统重新启动等等。我认为我需要将值存储在任何地方,以便我可以检查数字是否重复,但当列表超出限制时,它会太复杂。

6 个解决方案

#1


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

第一:如果保证数字永远不会重复,那就不是很随机了。

Second: There are lots of PRNG algorithms.

第二:有很多PRNG算法。

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.

第三:UUID有一个IETF RFC(MS调用GUID),但你应该认识到(U | G)UID在加密方面是不安全的,如果你担心的话。

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

这是.NET的GUID的文档

#2


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.

但是,还有其他方法可以获得随机数,这些方法涉及专用硬件以获得真正的随机数。我知道一些使用这种硬件的扑克网站。阅读他们如何做到这一点非常有趣。

#3


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.

如果这不可能,您还可以使用系统时钟初始化种子。

#4


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

您可以使用此代码示例:http://xkcd.com/221/或者,您可以使用本书: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.

但严重的是,不要自己实现它,使用现有的库。你不可能是第一个这样做的人。

#5


Specifically regarding Java:

特别是关于Java:

  • java.util.Random uses a linear congruential generator, which is not very good
  • java.util.Random使用线性同余生成器,这不是很好

  • 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.
  • java.util.UUID #randomUUID()使用java.security.SecureRandom,这是一个用于各种加密安全RNG的接口 - 我相信默认基于SHA-1。

  • UUIDs/GUIDs are not necessarily random
  • UUID / GUID不一定是随机的

  • 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
  • 在网上找到比java.util.Random好得多的RNG实现很容易,例如Mersenne Twister或乘法携带

#6


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

我知道您正在寻找一种使用C#生成随机数的方法。如果是,RNGCryptoServiceProvider就是你要找的。

[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!

如果使用RNGCryptoServiceProvider生成相当长的字节数,它可能是唯一的,但没有保证。理论上,真正的随机数并不意味着是唯一的。你掷了两次骰子,你可能会同时得到头,但它们仍然是随机的。真正的随机!

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.

我想应用唯一的检查,你只需要推出自己的机制来保存以前生成的数字的历史记录。

#1


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

第一:如果保证数字永远不会重复,那就不是很随机了。

Second: There are lots of PRNG algorithms.

第二:有很多PRNG算法。

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.

第三:UUID有一个IETF RFC(MS调用GUID),但你应该认识到(U | G)UID在加密方面是不安全的,如果你担心的话。

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

这是.NET的GUID的文档

#2


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.

但是,还有其他方法可以获得随机数,这些方法涉及专用硬件以获得真正的随机数。我知道一些使用这种硬件的扑克网站。阅读他们如何做到这一点非常有趣。

#3


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.

如果这不可能,您还可以使用系统时钟初始化种子。

#4


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

您可以使用此代码示例:http://xkcd.com/221/或者,您可以使用本书: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.

但严重的是,不要自己实现它,使用现有的库。你不可能是第一个这样做的人。

#5


Specifically regarding Java:

特别是关于Java:

  • java.util.Random uses a linear congruential generator, which is not very good
  • java.util.Random使用线性同余生成器,这不是很好

  • 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.
  • java.util.UUID #randomUUID()使用java.security.SecureRandom,这是一个用于各种加密安全RNG的接口 - 我相信默认基于SHA-1。

  • UUIDs/GUIDs are not necessarily random
  • UUID / GUID不一定是随机的

  • 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
  • 在网上找到比java.util.Random好得多的RNG实现很容易,例如Mersenne Twister或乘法携带

#6


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

我知道您正在寻找一种使用C#生成随机数的方法。如果是,RNGCryptoServiceProvider就是你要找的。

[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!

如果使用RNGCryptoServiceProvider生成相当长的字节数,它可能是唯一的,但没有保证。理论上,真正的随机数并不意味着是唯一的。你掷了两次骰子,你可能会同时得到头,但它们仍然是随机的。真正的随机!

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.

我想应用唯一的检查,你只需要推出自己的机制来保存以前生成的数字的历史记录。