【原创】开源Math.NET基础数学类库使用(12)C#随机数扩展方法
本博客所有文章分类的总目录:【总目录】本博客博文总目录-实时更新
开源Math.NET基础数学类库使用总目录:【目录】开源Math.NET基础数学类库使用总目录
前言
真正意义上的随机数(或者随机事件)在某次产生过程中是按照实验过程中表现的分布概率随机产生的,其结果是不可预测的,是不可见的。而计算机中的随机函数是按照一定算法模拟产生的,其结果是确定的,是可见的。我们可以这样认为这个可预见的结果其出现的概率是100%。所以用计算机随机函数所产生的“随机数”并不随机,是伪随机数。伪随机数的作用在开发中的使用非常常见,因此.NET在System命名空间,提供了一个简单的Random随机数生成类型。但这个类型并不能满足所有的需求,本节开始就将陆续介绍Math.NET中有关随机数的扩展以及其他伪随机生成算法编写的随机数生成器。
今天要介绍的是基于System.Random的扩展方法。
如果本文显示有问题,请参考:http://www.cnblogs.com/asxinyu/p/4301544.html
1.Random的扩展方法类
Rondom扩展及随机数相关的类都在Math.NET的MathNet.Numerics.Random命名空间,今天要介绍的 RandomExtensions 类是 扩展Random的静态方法类,可以直接在System.Random的对象上使用,相关功能介绍:
1.可以直接返回填充0-1随机数据的数组,如NextDoubles方法;
2.可以返回一个无限长度的IEnumerable接口对象,一直迭代返回double类型的随机数,是NextDoubleSequence方法;
3.类似的还可以返回其他类型的随机数据数组,如NextBytes,NextInt32s等;
4.还可以单独返回Int32类型和Int64类型的随机数,其范围在该类型的所有值域上,如NextFullRangeInt32,NextFullRangeInt64;
5.还可以单独返回Int32类型和Int64类型的随机数,其范围是该类型所有值域上的非负数,如NextInt64;
2.RandomExtensions类的实现
作为静态类,使用非常简单,为了方便理解,我将注释进行了部分翻译,贴出该类的所有源码,大家可以参考参考:
/// <summary>这个类是对System.Random类的扩展,扩展方法可以生成更多类型的伪随机数,而不是仅仅是double和Int32类型</summary>
/// <remarks>这个扩展是线程安全的,并且只有在Math.NET提供的随机数发生器或者RandomSource的继承类中被调用</remarks>
public static class RandomExtensions
{
/// <summary>使用(0-1)范围内的均匀随机数填充1个数组</summary>
/// <param name="rnd">Random类型的随机数生成器</param>
/// <param name="values">要填充随机数的数组</param>
/// <remarks>这个扩展是线程安全的,并且只有在Math.NET提供的随机数发生器或者RandomSource的继承类中被调用</remarks>
public static void NextDoubles(this System.Random rnd, double[] values)
{
var rs = rnd as RandomSource;
if (rs != null)
{
rs.NextDoubles(values);
return;
} for (var i = ; i < values.Length; i++)
{
values[i] = rnd.NextDouble();
}
} /// <summary>返回一个(0-1)范围内的均匀随机数填充1个数组</summary>
/// <param name="rnd">Random类型的随机数生成器</param>
/// <param name="count">要返回的数组的长度</param> public static double[] NextDoubles(this System.Random rnd, int count)
{
var values = new double[count];
NextDoubles(rnd, values);
return values;
} /// <summary>返回1个无限的0-1均匀分布随机数序列</summary>
public static IEnumerable<double> NextDoubleSequence(this System.Random rnd)
{
var rs = rnd as RandomSource;
if (rs != null) return rs.NextDoubleSequence();
return NextDoubleSequenceEnumerable(rnd);
} static IEnumerable<double> NextDoubleSequenceEnumerable(System.Random rnd)
{
while (true)
{
yield return rnd.NextDouble();
}
} /// <summary>返回1个均匀分布的byte数组</summary>
/// <param name="rnd">Random类型的随机数生成器</param>
/// <param name="count">要返回的数组的长度</param>
public static byte[] NextBytes(this System.Random rnd, int count)
{
var values = new byte[count];
rnd.NextBytes(values);
return values;
} /// <summary>
/// Fills an array with uniform random numbers greater than or equal to 0.0 and less than 1.0.
/// </summary>
/// <param name="rnd">The random number generator.</param>
/// <param name="values">The array to fill with random values.</param>
/// <param name="minInclusive">Lower bound, inclusive.</param>
/// <param name="maxExclusive">Upper bound, exclusive.</param>
public static void NextInt32s(this System.Random rnd, int[] values, int minInclusive, int maxExclusive)
{
var rs = rnd as RandomSource;
if (rs != null)
{
rs.NextInt32s(values, minInclusive, maxExclusive);
return;
}
for (var i = ; i < values.Length; i++)
{
values[i] = rnd.Next(minInclusive, maxExclusive);
}
} /// <summary>
/// Returns an infinite sequence of uniform random numbers greater than or equal to 0.0 and less than 1.0.
/// </summary>
public static IEnumerable<int> NextInt32Sequence(this System.Random rnd, int minInclusive, int maxExclusive)
{
var rs = rnd as RandomSource;
if (rs != null)
{
return rs.NextInt32Sequence(minInclusive, maxExclusive);
}
return NextInt32SequenceEnumerable(rnd, minInclusive, maxExclusive);
} static IEnumerable<int> NextInt32SequenceEnumerable(System.Random rnd, int minInclusive, int maxExclusive)
{
while (true)
{
yield return rnd.Next(minInclusive, maxExclusive);
}
} /// <summary>返回Int64类型的非负随机数</summary>
/// <param name="rnd">Random类型的随机数生成器</param>
/// <returns>
/// A 64-bit signed integer greater than or equal to 0, and less than <see cref="Int64.MaxValue"/>; that is,
/// the range of return values includes 0 but not <see cref="Int64.MaxValue"/>.
/// </returns>
/// <seealso cref="NextFullRangeInt64"/>
public static long NextInt64(this System.Random rnd)
{
var buffer = new byte[sizeof (long)]; rnd.NextBytes(buffer);
var candidate = BitConverter.ToInt64(buffer, ); candidate &= long.MaxValue;
return (candidate == long.MaxValue) ? rnd.NextInt64() : candidate;
} /// <summary>
/// Returns a random number of the full Int32 range.
/// </summary>
/// <param name="rnd">The random number generator.</param>
/// <returns>
/// A 32-bit signed integer of the full range, including 0, negative numbers,
/// <see cref="Int32.MaxValue"/> and <see cref="Int32.MinValue"/>.
/// </returns>
/// <seealso cref="System.Random.Next()"/>
public static int NextFullRangeInt32(this System.Random rnd)
{
var buffer = new byte[sizeof (int)];
rnd.NextBytes(buffer);
return BitConverter.ToInt32(buffer, );
} /// <summary>
/// Returns a random number of the full Int64 range.
/// </summary>
/// <param name="rnd">The random number generator.</param>
/// <returns>
/// A 64-bit signed integer of the full range, including 0, negative numbers,
/// <see cref="Int64.MaxValue"/> and <see cref="Int64.MinValue"/>.
/// </returns>
/// <seealso cref="NextInt64"/>
public static long NextFullRangeInt64(this System.Random rnd)
{
var buffer = new byte[sizeof (long)];
rnd.NextBytes(buffer);
return BitConverter.ToInt64(buffer, );
} /// <summary>
/// Returns a nonnegative decimal floating point random number less than 1.0.
/// </summary>
/// <param name="rnd">The random number generator.</param>
/// <returns>
/// A decimal floating point number greater than or equal to 0.0, and less than 1.0; that is,
/// the range of return values includes 0.0 but not 1.0.
/// </returns>
public static decimal NextDecimal(this System.Random rnd)
{
decimal candidate; // 50.049 % chance that the number is below 1.0. Try until we have one.
// Guarantees that any decimal in the interval can
// indeed be reached, with uniform probability.
do
{
candidate = new decimal(
rnd.NextFullRangeInt32(),
rnd.NextFullRangeInt32(),
rnd.NextFullRangeInt32(),
false,
);
}
while (candidate >= 1.0m); return candidate;
}
}
其使用非常简单,这里就不再举例子。这种扩展大家也应该写过,后面几篇文章将介绍Math.NET中实现的其他算法的随机数发生器。请关注
3.资源
源码下载:http://www.cnblogs.com/asxinyu/p/4264638.html
如果本文显示有问题,请参考本文原文:http://www.cnblogs.com/asxinyu/p/4301544.html
【原创】开源Math.NET基础数学类库使用(12)C#随机数扩展方法的更多相关文章
- 开源Math.NET基础数学类库使用(12)C#随机数扩展方法
原文:[原创]开源Math.NET基础数学类库使用(12)C#随机数扩展方法 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...
- 【目录】开源Math.NET基础数学类库使用总目录
本博客所有文章分类的总目录链接:[总目录]本博客博文总目录-实时更新 1.开源Math.NET数学组件文章 1.开源Math.NET基础数学类库使用(01)综合介绍 2.开源Math.NET ...
- 【原创】开源Math.NET基础数学类库使用(02)矩阵向量计算
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 【原创】开源Math.NET基础数学类库使用(07)常用的数学物理常数
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 1.前 ...
- 【原创】开源Math.NET基础数学类库使用(09)相关数论函数使用
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 【原创】开源Math.NET基础数学类库使用(01)综合介绍
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 【原创】开源Math.NET基础数学类库使用(03)C#解析Matlab的mat格式
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 【原创】开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 【原创】开源Math.NET基础数学类库使用(05)C#解析Delimited Formats数据格式
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
随机推荐
- js实现简单的图片轮播
js代码如下 <script type="text/javascript"> var n=1; var map=new Array(); map[0]=new Imag ...
- bootstrap学习笔记【转】
bootstrap是由Twitter公司研发的一个基于HTML,CSS,JavaScript的开源框架,最重要的部分是它的响应式布局.(国内文档翻译官网:http://www.bootcss.com/ ...
- 多线程、委托、Invoke解决winform界面卡死的问题,并带开关
一.知识点介绍 1,更新控件的内容,应该调用控件的Invoke方法. Invoke指: 在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托.该方法接收一个委托类型和委托的参数,因此需要定义 ...
- ajax内调用WCF服务
WCF可以当作WebService一样被调用,在html内通过ajax调用WCF服务的方法如下: 1.新建一个WCF服务的网站项目: 2.在项目内增加一个新项:启用了ajax的WCF服务: 3.在对应 ...
- sqlplus运行sql文件
当sql文件的数据比较多的时候,pl/sql运行比较慢,可以通过oracle的sqlplus进行导入: sqlplus user/password@tnsname@sqlfile.sql; 注意如果文 ...
- 解决:No module named pkg_resources
今天在装bleach的时候,发现bleach依赖的其中的一个库是html5lib,从pypi下载源码包,执行python setup.py install,报错了: 提示我的setuptools版本过 ...
- Python之路第一课Day4--随堂笔记(迭代生成装饰器)
上节回顾: 1.集合 a.关系测试 b.去重 2.文件操作及编码 3.函数 4.局部变量和全局变量 上节回顾 本节课内容: 1.迭代器生成器 2.装饰器 3.json pickle数据序列化 4.软件 ...
- 在Excel中把横行与竖列进行置换、打勾号
在Excel中把横行与竖列进行置换:复制要置换的单元,在新的单元上右键->选择性复制,会出现对话框,选中“置换”,即可在Excel中打勾号,左手按住ALT不放,右手在小键盘也就是右边的数字键盘依 ...
- Android课程---序列化与反序列化(转)
ava序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?本文围绕这些问题进行了探讨. 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列的 ...
- java学习之接口、多态和内部类
接口是一组行为的规范.定义.接口是面向对象编程体系中的思想精髓之一,使用接口可以让我们的程序更加利于变化. 接口的格式: interface 接口名称{ 全局变量: 抽象方法: } 接口中的成员修饰符 ...