且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

并行循环和随机产生奇怪的结果

更新时间:2023-01-07 14:01:03

这两种方法都不能为您提供非常好的随机数.

Neither of these approaches will give you really good random numbers.

这篇博客文章介绍了许多使用Random获得更好随机数的方法

This blog post covers a lot of approaches for getting better random numbers with Random

http://blogs.msdn.com /b/pfxteam/archive/2009/02/19/9434171.aspx

对于许多日常应用来说,这些可能很好.

These may be fine for many day to day applications.

但是,如果在多个线程上使用相同的随机数生成器,甚至具有不同的种子,则仍然会影响随机数的质量.这是因为您正在生成可能重叠的伪随机数序列.

However if you use the same random number generator on multiple threads even with different seeds you will still impact the quality of your random numbers. This is because you are generating sequences of pseudo-random numbers which may overlap.

此视频详细解释了原因:

This video explains why in a bit more detail:

http: //software.intel.com/zh-CN/videos/tim-mattson-use-and-abuse-of-random-numbers/

如果您真的想要随机数,那么您确实需要使用加密随机数生成器System.Security.Cryptography.RNGCryptoServiceProvider.这是线程安全的.

If you want really random numbers then you really need to use the crypto random number generator System.Security.Cryptography.RNGCryptoServiceProvider. This is threadsafe.