.Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

.Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数:

            for (int i = 0; i < 10; i++)
            {
                Random random1 = new Random();
                Console.WriteLine(random1.Next());
            }

测试生成随时基本都是相同的结果:

很显然上面的结果是不靠谱的,为什么会这样呢,因为微软的Random类,发现在C#中生成随机数使用的算法是线性同余法,这种算法生成的不是绝对随机,而是一种伪随机数,线性同余法算法的的公式是

:第n+1个数 = ( 第N个数 * a + b) % m ,公式中a、b和m分别为常数,是生成随机数的因子,如果之前从未通过同一个Random对象生成过随机数(也就是调用过Next方法),那么第N个随机数为将被指定为一个默认的常数,这个常数在创建一个Random类时被默认值指定,Random也提供一个构造函数允许开发者使用自己的随机数因子.

有人说要将 Random random1 = new Random(); 要放到循环的外面:

            Random random2 = new Random();
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(random2.Next());
            }

测试上面的代码执行的结果是这样的:

得到结果还是不靠谱的

有人说使用GUID产生填充因子:

            for (int i = 0; i < 10; i++)
            {
                byte[] buffer = Guid.NewGuid().ToByteArray();
                int iSeed = BitConverter.ToInt32(buffer, 0);
                Random random3 = new Random(iSeed);
                Console.WriteLine(random3.Next());
            }

测试上面的代码得到的结果:

得到的结果还是不靠谱的。

为了生成更加可靠的随机数,微软在System.Security.Cryptography命名空间下提供一个名为system.Security.Cryptography.RNGCryptoServiceProvider的类,它采用系统当前的硬件信息、进程信息、线程信息、系统启动时间和当前精确时间作为填充因子,通过更好的算法生成高质量的随机数,它的使用方法如下所示:

            for (int i = 0; i < 20; i++)
            {
                byte[] randomBytes = new byte[8];
                System.Security.Cryptography.RNGCryptoServiceProvider rngServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider();
                rngServiceProvider.GetBytes(randomBytes);
                int result = BitConverter.ToInt32(randomBytes, 0);
                result = System.Math.Abs(result);  //求绝对值
                Console.WriteLine(result);
            }

测试结果未发现重复的:

总结:

Random算法简单,性能较高,适用于随机性要求不高的情况,由于RNGCryptoServiceProvider在生成期间需要查询上面提到的几种系统因子,所以性能稍弱于Random类,但随机数质量高,可靠性更好。使用哪一种方式视情况而定

时间: 2024-07-30 10:13:24

.Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数的相关文章

转:system.Security.Cryptography C# 加密和解密

以下文转自: http://www.360doc.com/content/13/0122/05/19147_261678471.shtml 总结:注册的时候经过MD5加密存进数据库,在登录的时候需要先加密输入的密码,再进行和数据库里的比对,因为同一字符串加密后是一样的,并不是无规则的:实例: string name = this.TextBox1.Text;        string pwd = System.Web.Security.FormsAuthentication.HashPassw

部署时,出现用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法

转载:http://www.cnblogs.com/jys509/p/4499978.html 在调用RSA加密的.pfx密钥时,在本地调试没有问题,可以布署到服务器,就会报以下的错误: 用户代码未处理 System.Security.Cryptography.CryptographicException HResult=-2146893792 Message=出现了内部错误. Source=mscorlib StackTrace: 在 System.Security.Cryptography.

WebApi 数据保护操作未成功。这可能是由于未为当前线程的用户上下文加载用户配置文件导致的。当线程执行模拟时,可能会出现此情况。&quot;,&quot;ExceptionType&quot;:&quot;System.Security.Cryptography.CryptographicException&quot;,&quot;StackTrace

在调用System.Security.Cryptography.ProtectedData.Protect方法来保护私密信息时,IIS可能会报以下错误:CryptographicException: 数据保护操作未成功.这可能是由于未为当前线程的用户上下文加载用户配置文件导致的.当线程执行模拟时,可能会出现此情况. 解决方法:1.打开应用程序池2.在应用程序上右键选择高级设置3.进程模型下设置“加载用户配置文件”为True

.NET:System.Security.Cryptography.CryptographicException 的解决办法

问题描述:微信退款提示加载证书失败问题(System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile) 详细内容参考此网址:http://social.msdn.microsoft.com/Forums/en-US/ec93922a-fd1e-4225-b5cf-1472ebb3acd1/systemsecuritycryptographycryptographicexception-the-system-ca

使用证书部署出现System.Security.Cryptography.CryptographicException 错误解决方案

一.System.Security.Cryptography.CryptographicException: 找不到对象 at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, In

C#原生加密方法: System.Security.Cryptography.CryptoStream DataSet加密解密

采用16位密钥形式加密,把数据 dataset或文本转换为二进制流,然后进行加密解密.代码如下: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace CryptoHelperLib { publ

&quot;System.Security.Cryptography.CryptographicException: 拒绝访问&quot; 问题的解决方法

.net web程序使用rsa算法进行加解密时,程序报告“System.Security.Cryptography.CryptographicException: 拒绝访问”错.按网上搜的解决方法做了各种权限配置,然并卵.试了两天终于解决,现记录如下: C:\Documents and Settings\All Users\Application Data 添加项目应用程序池对应启动帐号(或iis_wpg组) 读取,读取并运行,列出文件夹目录权限. C:\Documents and Settin

System.Security.Cryptography.CryptographicException: 出现了内部错误

(转载: http://www.cnblogs.com/ithome8/p/5189926.html) 调用微信退款接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法 我总结了一下出现证书无法加载的原因有以下三个 1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置--&g

System.Security.Cryptography.RSA.FromXmlString 系统找不到指定的文件和X509读取证书文件系统找不到指定的文件异常

前言: 最近公司增加服务器,在新增加的服务器中发现一些问题. 1.应用程序在读取证书文件中出现"系统找不到指定的文件."异常,但是已经确认证书文件存在.本地测试也可以读取,就在新增加的服务器上不可以. 2.RSA加密过程中在 System.Security.Cryptography.RSA.FromXmlString()抛异常: System.Security.Cryptography.CryptographicException: 系统找不到指定的文件. 解决方案: 打开IIS设置,