本博客所有文章分类的总目录:【总目录】本博客博文总目录-实时更新 

开源Math.NET基础数学类库使用总目录:【目录】开源Math.NET基础数学类库使用总目录

前言

  真正意义上的随机数(或者随机事件)在某次产生过程中是按照实验过程中表现的分布概率随机产生的,其结果是不可预测的,是不可见的。而计算机中的随机函数是按照一定算法模拟产生的,其结果是确定的,是可见的。我们可以这样认为这个可预见的结果其出现的概率是100%。所以用计算机随机函数所产生的“随机数”并不随机,是伪随机数。伪随机数的作用在开发中的使用非常常见,因此.NET在System命名空间,提供了一个简单的Random随机数生成类型。但这个类型并不能满足所有的需求,本节开始就将陆续介绍Math.NET中有关随机数的扩展以及其他伪随机生成算法编写的随机数生成器。

  今天要介绍的是Math.NET中利用C#快速的生成安全的随机数。

  如果本文资源或者显示有问题,请参考 本文原文地址:http://www.cnblogs.com/asxinyu/p/4301554.html

1.什么是安全的随机数?

  Math.NET在MathNet.Numerics.Random命名空间中的实现了一个基于System.Security.Cryptography.RandomNumberGenerator的安全随机数发生器。

  实际使用中,很多人对这个不在意,那么Random和安全的随机数有什么区别,什么是安全的随机数呢?

  在许多类型软件的开发过程中,都要使用随机数。例如纸牌的分发、密钥的生成等等。随机数至少应该具备两个条件:
1. 数字序列在统计上是随机的。
2. 不能通过已知序列来推算后面未知的序列。
  只有实际物理过程才是真正随机的。而一般来说,计算机是很确定的,它很难得到真正的随机数。所以计算机利用设计好的一套算法,再由用户提供一个种子值,得出被称为“伪随机数”的数字序列,这就是我们平时所使用的随机数。
这种伪随机数字足以满足一般的应用,但它不适用于加密等领域,因为它具有弱点:
1. 伪随机数是周期性的,当它们足够多时,会重复数字序列。
2. 如果提供相同的算法和相同的种子值,将会得出完全一样的随机数序列。
3. 可以使用逆向工程,猜测算法与种子值,以便推算后面所有的随机数列。

  对于这个随机数发生器,本人深有体会,在研究生期间,我的研究方向就是 流密码,其中一个主要的课题就是 如何生成高安全性能的随机数发生器,研究了2年吧,用的是 混沌生成伪随机数,用于加密算法。.NET自带的Random类虽然能满足常规要求,但在一些高安全场合,是不建议使用的,因为其生成的随机数是可以预测和破解的。所以在.net中也提供了一个用于加密的RandomNumberGenerator。Math.NET就是该类的一个翻版。虽然其效率要比Random更低,但是更安全。

2..NET中使用RNGCryptoServiceProvider的例子

 RNGCryptoServiceProvider的使用可以参考一个MSDN的例子:  

 1 using System;
 2 using System.IO;
 3 using System.Text;
 4 using System.Security.Cryptography;
 5
 6 class RNGCSP
 7 {
 8     public static void Main()
 9     {
10         for(int x = 0; x <= 30; x++)
11             Console.WriteLine(RollDice(6));
12     }
13
14     public static int RollDice(int NumSides)
15     {
16         byte[] randomNumber = new byte[1];
17
18         RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
19
20         Gen.GetBytes(randomNumber);
21
22         int rand = Convert.ToInt32(randomNumber[0]);
23
24         return rand % NumSides + 1;
25     }
26 }

3.Math.NET中安全随机数类的实现

  随机数生成器算法的实现基本都类似,这里就看一下Math.NET中安全的随机数生成器CryptoRandomSource类的实现:

  1 public sealed class CryptoRandomSource : RandomSource, IDisposable
  2 {
  3     const double Reciprocal = 1.0/uint.MaxValue;
  4     readonly RandomNumberGenerator _crypto;
  5
  6     /// <summary>
  7     /// Construct a new random number generator with a random seed.
  8     /// </summary>
  9     /// <remarks>Uses <see cref="System.Security.Cryptography.RNGCryptoServiceProvider"/> and uses the value of
 10     /// <see cref="Control.ThreadSafeRandomNumberGenerators"/> to set whether the instance is thread safe.</remarks>
 11     public CryptoRandomSource()
 12     {
 13         _crypto = new RNGCryptoServiceProvider();
 14     }
 15
 16     /// <summary>
 17     /// Construct a new random number generator with random seed.
 18     /// </summary>
 19     /// <param name="rng">The <see cref="RandomNumberGenerator"/> to use.</param>
 20     /// <remarks>Uses the value of  <see cref="Control.ThreadSafeRandomNumberGenerators"/> to set whether the instance is thread safe.</remarks>
 21     public CryptoRandomSource(RandomNumberGenerator rng)
 22     {
 23         _crypto = rng;
 24     }
 25
 26     /// <summary>
 27     /// Construct a new random number generator with random seed.
 28     /// </summary>
 29     /// <remarks>Uses <see cref="System.Security.Cryptography.RNGCryptoServiceProvider"/></remarks>
 30     /// <param name="threadSafe">if set to <c>true</c> , the class is thread safe.</param>
 31     public CryptoRandomSource(bool threadSafe) : base(threadSafe)
 32     {
 33         _crypto = new RNGCryptoServiceProvider();
 34     }
 35
 36     /// <summary>
 37     /// Construct a new random number generator with random seed.
 38     /// </summary>
 39     /// <param name="rng">The <see cref="RandomNumberGenerator"/> to use.</param>
 40     /// <param name="threadSafe">if set to <c>true</c> , the class is thread safe.</param>
 41     public CryptoRandomSource(RandomNumberGenerator rng, bool threadSafe) : base(threadSafe)
 42     {
 43         _crypto = rng;
 44     }
 45
 46     /// <summary>
 47     /// Returns a random number between 0.0 and 1.0.
 48     /// </summary>
 49     /// <returns>
 50     /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
 51     /// </returns>
 52     protected override sealed double DoSample()
 53     {
 54         var bytes = new byte[4];
 55         _crypto.GetBytes(bytes);
 56         return BitConverter.ToUInt32(bytes, 0)*Reciprocal;
 57     }
 58
 59     public void Dispose()
 60     {
 61 #if !NET35
 62         _crypto.Dispose();
 63 #endif
 64     }
 65
 66     /// <summary>
 67     /// Fills an array with random numbers greater than or equal to 0.0 and less than 1.0.
 68     /// </summary>
 69     /// <remarks>Supports being called in parallel from multiple threads.</remarks>
 70     public static void Doubles(double[] values)
 71     {
 72         var bytes = new byte[values.Length*4];
 73
 74 #if !NET35
 75         using (var rnd = new RNGCryptoServiceProvider())
 76         {
 77             rnd.GetBytes(bytes);
 78         }
 79 #else
 80         var rnd = new RNGCryptoServiceProvider();
 81         rnd.GetBytes(bytes);
 82 #endif
 83
 84         for (int i = 0; i < values.Length; i++)
 85         {
 86             values[i] = BitConverter.ToUInt32(bytes, i*4)*Reciprocal;
 87         }
 88     }
 89
 90     /// <summary>
 91     /// Returns an array of random numbers greater than or equal to 0.0 and less than 1.0.
 92     /// </summary>
 93     /// <remarks>Supports being called in parallel from multiple threads.</remarks>
 94     [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
 95     public static double[] Doubles(int length)
 96     {
 97         var data = new double[length];
 98         Doubles(data);
 99         return data;
100     }
101
102     /// <summary>
103     /// Returns an infinite sequence of random numbers greater than or equal to 0.0 and less than 1.0.
104     /// </summary>
105     /// <remarks>Supports being called in parallel from multiple threads.</remarks>
106     public static IEnumerable<double> DoubleSequence()
107     {
108         var rnd = new RNGCryptoServiceProvider();
109         var buffer = new byte[1024*4];
110
111         while (true)
112         {
113             rnd.GetBytes(buffer);
114             for (int i = 0; i < buffer.Length; i += 4)
115             {
116                 yield return BitConverter.ToUInt32(buffer, i)*Reciprocal;
117             }
118         }
119     }
120 }

4.资源

  源码下载:http://www.cnblogs.com/asxinyu/p/4264638.html

  如果本文资源或者显示有问题,请参考 本文原文地址:http://www.cnblogs.com/asxinyu/p/4301519.html

【原创】开源Math.NET基础数学类库使用(14)C#生成安全的随机数相关推荐

  1. 【原创】开源Math.NET基础数学类库使用(06)直接求解线性方程组

    阅读目录 前言 1.数值分析与线性方程 2.Math.NET解线性方程源码分析 3.Math.NET求解线性方程的实例 4.资源                本博客所有文章分类的总目录:[总目录]本 ...

  2. 【原创】开源Math.NET基础数学类库使用(05)C#解析Delimited Formats数据格式

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...

  3. 【原创】开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...

  4. 开源Math.NET基础数学类库使用(13)C#实现其他随机数生成器

    原文:[原创]开源Math.NET基础数学类库使用(13)C#实现其他随机数生成器                本博客所有文章分类的总目录:http://www.cnblogs.com/asxiny ...

  5. 开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式

    原文:[原创]开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式 开源Math.NET基础数学类库使用系列文章总目录:   1.开源.NET基础数学计算组件Math. ...

  6. 开源Math.NET基础数学类库使用(03)C#解析Matlab的mat格式

    开源Math.NET基础数学类库使用(03)C#解析Matlab的mat格式 原文:[原创]开源Math.NET基础数学类库使用(03)C#解析Matlab的mat格式 开源Math.NET基础数学类 ...

  7. 开源Math.NET基础数学类库使用(11)C#计算相关系数

    阅读目录 前言 1.Math.NET计算相关系数的类 2.Correlation的实现 3.使用案例 4.资源                本博客所有文章分类的总目录:[总目录]本博客博文总目录-实 ...

  8. 开源Math.NET基础数学类库使用(01)综合介绍

    该文章为转载文章,原文文章地址,请点击此处. 前言 几年前接触这个组件的时候,只需要在.NET平台进行一些常规的微积分计算,功能还比较少,只限于常规的数值计算,现在已经功能越来越多了,应该是目前最好的 ...

  9. 2 开源Math.NET基础数学类库使用矩阵向量计算

    前言 本文开始一一介绍Math.NET的几个主要子项目的相关功能的使用.今天先要介绍的是最基本Math.NET Numerics的最基本矩阵与向量计算. 1.创建Numerics矩阵与向量 矩阵与向量 ...

最新文章

  1. wince5使用access数据库_关于wince系统支持什么数据库的阿里云论坛用户知识和技术交流...
  2. linux中将文本中的单词换掉的指令_从零开始学Linux运维|19.文本处理相关命令(2)...
  3. c语言printout函数,只使用处理I/O的PrintDigit函数,编写一个过程以输出任意实数...
  4. git报ssh variant 'simple' does not support setting port解决办法
  5. Python(2):基本数据类型
  6. 《Android 游戏开发大全(第二版)》——6.4节角色扮演游戏
  7. 简单谈谈5G/C-V2X技术与自动驾驶的关系
  8. 学会查找问题的源头:《全屏游戏中自动切出到桌面的问题解决(二)》
  9. Windows下React Native开发01 -- Android开发环境搭建
  10. Promise API 简介
  11. paip.python错误解决10
  12. [追加评论]三款SDR平台对比:HackRF,bladeRF和USRP
  13. fanuc以太网参数设置视频_fanuc-mf系统 以太网设置方法资料
  14. android 入门记录
  15. WCF+SQL Server 2008 明源售楼系统项目解析
  16. 7个高清图片素材网,免费/可商用
  17. Rust - Pin | Unpin | PhantomPinned
  18. 大吉大利,今晚吃鸡——跑毒篇
  19. 一个leader,要有角色认知
  20. 爬虫入门3---爬虫实战

热门文章

  1. .Net运行时的相互关系
  2. 泛海精灵的用户分析:补充【Song Xie】
  3. 将Excel数据导入SQL Server数据库
  4. C语言经典例26-利用递归方法求阶乘
  5. 【Groovy】Groovy 扩展方法 ( Groovy 扩展方法引入 | 分析 Groovy 中 Thread 类的 start 扩展方法 )
  6. 【Android 逆向】x86 汇编 ( 使用 IDA 解析 x86 架构的动态库文件 | 使用 IDA 打开动态库文件 | IDA 中查找指定的方法 )
  7. 【Android 插件化】Hook 插件化框架 ( Hook Activity 启动过程 | 静态代理 )
  8. 【DBMS 数据库管理系统】数据库 -> 数据仓库 ( 数据处理类型 | 传统数据库 | 数据库不适用于分析型应用 )
  9. 【Android RTMP】NV21 图像旋转处理 ( 问题描述 | 图像顺时针旋转 90 度方案 | YUV 图像旋转细节 | 手机屏幕旋转方向 )
  10. 【Android FFMPEG 开发】FFMPEG 视频播放进度控制 ( 显示播放进度 | 拖动进度条播放 )