我怎样才能产生真正的(不是伪)随机数与C#?随机数、不是

2023-09-10 23:04:58 作者:无声情话

我知道,Random类可产生伪随机数,但有什么办法来产生真正的随机数?

I know that the Random class can generate pseudo-random numbers but is there a way to generate truly random numbers?

推荐答案

答案在这里有它的两个主要方面。有一些非常重要的微妙之处,你应该给予应有的重视......

The answer here has two main sides to it. There are some quite important subtleties to which you should pay due attention...

的RNGCryptoServiceProvider,这是首创置业的加密API的一部分,应该做的工作适合你。它仍然是技术上所产生的伪随机数,但随机性的质量高得多。 - 适于加密的目的,正如其名称可能暗示

The RNGCryptoServiceProvider, which is part of the Crypto API in the BCL, should do the job for you. It's still technically a pseudo-random number generated, but the quality of "randomness" is much higher - suitable for cryptographic purposes, as the name might suggest.

有其他crypographic的API提供高品质的伪随机generaters可用了。算法如梅森倍捻机很受欢迎。

There are other crypographic APIs with high quality pseudo random generaters available too. Algorithms such as the Mersenne twister are quite popular.

比较这对随机类的BCL,这是显著更好。如果阴谋随机在图上产生的数字,例如,你应该能够识别模式,这是一种软弱的强有力的信号。这主要是由于该算法只需使用固定大小的晶种查找表这一事实。

Comparing this to the Random class in the BCL, it is significantly better. If you plot the numbers generated by Random on a graph, for example, you should be able to recognise patterns, which is a strong sign of weakness. This is largely due to the fact that the algorithm simply uses a seeded lookup table of fixed size.

要生成的真正的随机数字,你需要利用一些自然现象,如核衰变,微小的温度波动(CPU温度比较conveient源),仅举几例。然而,这是更困难的,需要当然额外的硬件。我怀疑实际的解决方案( RNGCryptoServiceProvider 或等)应该做的工作完全满足您的需要。

To generate truly random numbers, you need to make use of some natural phenomenon, such as nuclear decay, microscopic temperature fluctuations (CPU temperature is a comparatively conveient source), to name a few. This however is much more difficult and requires additional hardware, of course. I suspect the practical solution (RNGCryptoServiceProvider or such) should do the job perfectly well for you.

现在,请注意,如果你的确实需要真正的随机数的,你可以使用的服务,如的 Random.org 的,生成数字具有非常高的随机性/熵(基于的大气噪声的)。数据是可免费下载。这可能仍然是不必要地复杂化您的具体情况,但它肯定给你适合科学研究和诸如此类的数据。

Now, note that if you really do require truly random numbers, you could use a service such as Random.org, which generates numbers with very high randomness/entropy (based on atmospheric noise). Data is freely available for download. This may nonetheless be unnecessarily complicated for your situation, although it certainly gives you data suitable for scientific study and whatnot.

选择是你到了最后,但至少你现在应该能够做出一个信息决策,意识到随机数发生器的各种类型和级别的。

The choice is yours in the end, but at least you should now be able to make an informative decision, being aware of the various types and levels of RNGs.

 
精彩推荐