在寻找生成真正随机数的最佳try 时,我偶然发现了这个代码示例.
寻找对这个片段的意见.
using System;
using System.Security.Cryptography;
private static int NextInt(int min, int max)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buffer = new byte[4];
rng.GetBytes(buffer);
int result = BitConverter.ToInt32(buffer, 0);
return new Random(result).Next(min, max);
}
来源:http://www.vcskicks.com/code-snippet/rng-int.php
这是否比使用滴答计数种子更可取,例如:
Random rand = new Random(Environment.TickCount);
rand.Next(min, max);
Note:个
我不是在寻找第三方随机数据提供者,比如Random.org,因为这样的依赖关系对应用程序来说是不现实的.