【原创】开源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类的实现

  作为静态类,使用非常简单,为了方便理解,我将注释进行了部分翻译,贴出该类的所有源码,大家可以参考参考: 

复制代码
  1 /// <summary>这个类是对System.Random类的扩展,扩展方法可以生成更多类型的伪随机数,而不是仅仅是double和Int32类型</summary>
  2 /// <remarks>这个扩展是线程安全的,并且只有在Math.NET提供的随机数发生器或者RandomSource的继承类中被调用</remarks>
  3 public static class RandomExtensions
  4 {
  5     /// <summary>使用(0-1)范围内的均匀随机数填充1个数组</summary>
  6     /// <param name="rnd">Random类型的随机数生成器</param>
  7     /// <param name="values">要填充随机数的数组</param>
  8     /// <remarks>这个扩展是线程安全的,并且只有在Math.NET提供的随机数发生器或者RandomSource的继承类中被调用</remarks>
  9     public static void NextDoubles(this System.Random rnd, double[] values)
 10     {
 11         var rs = rnd as RandomSource;
 12         if (rs != null)
 13         {
 14             rs.NextDoubles(values);
 15             return;
 16         }
 17 
 18         for (var i = 0; i < values.Length; i++)
 19         {
 20             values[i] = rnd.NextDouble();
 21         }
 22     }
 23 
 24     /// <summary>返回一个(0-1)范围内的均匀随机数填充1个数组</summary>
 25     /// <param name="rnd">Random类型的随机数生成器</param>
 26     /// <param name="count">要返回的数组的长度</param>
 27    
 28     public static double[] NextDoubles(this System.Random rnd, int count)
 29     {
 30         var values = new double[count];
 31         NextDoubles(rnd, values);
 32         return values;
 33     }
 34 
 35     /// <summary>返回1个无限的0-1均匀分布随机数序列</summary>
 36     public static IEnumerable<double> NextDoubleSequence(this System.Random rnd)
 37     {
 38         var rs = rnd as RandomSource;
 39         if (rs != null) return rs.NextDoubleSequence();
 40         return NextDoubleSequenceEnumerable(rnd);
 41     }
 42 
 43     static IEnumerable<double> NextDoubleSequenceEnumerable(System.Random rnd)
 44     {
 45         while (true)
 46         {
 47             yield return rnd.NextDouble();
 48         }
 49     }
 50 
 51     /// <summary>返回1个均匀分布的byte数组</summary>
 52     /// <param name="rnd">Random类型的随机数生成器</param>
 53     /// <param name="count">要返回的数组的长度</param>
 54     public static byte[] NextBytes(this System.Random rnd, int count)
 55     {
 56         var values = new byte[count];
 57         rnd.NextBytes(values);
 58         return values;
 59     }
 60 
 61     /// <summary>
 62     /// Fills an array with uniform random numbers greater than or equal to 0.0 and less than 1.0.
 63     /// </summary>
 64     /// <param name="rnd">The random number generator.</param>
 65     /// <param name="values">The array to fill with random values.</param>
 66     /// <param name="minInclusive">Lower bound, inclusive.</param>
 67     /// <param name="maxExclusive">Upper bound, exclusive.</param>
 68     public static void NextInt32s(this System.Random rnd, int[] values, int minInclusive, int maxExclusive)
 69     {
 70         var rs = rnd as RandomSource;
 71         if (rs != null)
 72         {
 73             rs.NextInt32s(values, minInclusive, maxExclusive);
 74             return;
 75         }
 76         for (var i = 0; i < values.Length; i++)
 77         {
 78             values[i] = rnd.Next(minInclusive, maxExclusive);
 79         }
 80     }
 81 
 82     /// <summary>
 83     /// Returns an infinite sequence of uniform random numbers greater than or equal to 0.0 and less than 1.0.
 84     /// </summary>
 85     public static IEnumerable<int> NextInt32Sequence(this System.Random rnd, int minInclusive, int maxExclusive)
 86     {
 87         var rs = rnd as RandomSource;
 88         if (rs != null)
 89         {
 90             return rs.NextInt32Sequence(minInclusive, maxExclusive);
 91         }
 92         return NextInt32SequenceEnumerable(rnd, minInclusive, maxExclusive);
 93     }
 94 
 95     static IEnumerable<int> NextInt32SequenceEnumerable(System.Random rnd, int minInclusive, int maxExclusive)
 96     {
 97         while (true)
 98         {
 99             yield return rnd.Next(minInclusive, maxExclusive);
100         }
101     }
102 
103     /// <summary>返回Int64类型的非负随机数</summary>
104     /// <param name="rnd">Random类型的随机数生成器</param>
105     /// <returns>
106     /// A 64-bit signed integer greater than or equal to 0, and less than <see cref="Int64.MaxValue"/>; that is, 
107     /// the range of return values includes 0 but not <see cref="Int64.MaxValue"/>.
108     /// </returns>
109     /// <seealso cref="NextFullRangeInt64"/>
110     public static long NextInt64(this System.Random rnd)
111     {
112         var buffer = new byte[sizeof (long)];
113 
114         rnd.NextBytes(buffer);
115         var candidate = BitConverter.ToInt64(buffer, 0);
116 
117         candidate &= long.MaxValue;
118         return (candidate == long.MaxValue) ? rnd.NextInt64() : candidate;
119     }
120 
121     /// <summary>
122     /// Returns a random number of the full Int32 range.
123     /// </summary>
124     /// <param name="rnd">The random number generator.</param>
125     /// <returns>
126     /// A 32-bit signed integer of the full range, including 0, negative numbers,
127     /// <see cref="Int32.MaxValue"/> and <see cref="Int32.MinValue"/>.
128     /// </returns>
129     /// <seealso cref="System.Random.Next()"/>
130     public static int NextFullRangeInt32(this System.Random rnd)
131     {
132         var buffer = new byte[sizeof (int)];
133         rnd.NextBytes(buffer);
134         return BitConverter.ToInt32(buffer, 0);
135     }
136 
137     /// <summary>
138     /// Returns a random number of the full Int64 range.
139     /// </summary>
140     /// <param name="rnd">The random number generator.</param>
141     /// <returns>
142     /// A 64-bit signed integer of the full range, including 0, negative numbers,
143     /// <see cref="Int64.MaxValue"/> and <see cref="Int64.MinValue"/>.
144     /// </returns>
145     /// <seealso cref="NextInt64"/>
146     public static long NextFullRangeInt64(this System.Random rnd)
147     {
148         var buffer = new byte[sizeof (long)];
149         rnd.NextBytes(buffer);
150         return BitConverter.ToInt64(buffer, 0);
151     }
152 
153     /// <summary>
154     /// Returns a nonnegative decimal floating point random number less than 1.0.
155     /// </summary>
156     /// <param name="rnd">The random number generator.</param>
157     /// <returns>
158     /// A decimal floating point number greater than or equal to 0.0, and less than 1.0; that is, 
159     /// the range of return values includes 0.0 but not 1.0.
160     /// </returns>
161     public static decimal NextDecimal(this System.Random rnd)
162     {
163         decimal candidate;
164 
165         // 50.049 % chance that the number is below 1.0. Try until we have one.
166         // Guarantees that any decimal in the interval can
167         // indeed be reached, with uniform probability.
168         do
169         {
170             candidate = new decimal(
171                 rnd.NextFullRangeInt32(),
172                 rnd.NextFullRangeInt32(),
173                 rnd.NextFullRangeInt32(),
174                 false,
175                 28);
176         }
177         while (candidate >= 1.0m);
178 
179         return candidate;
180     }
181 }
复制代码

  其使用非常简单,这里就不再举例子。这种扩展大家也应该写过,后面几篇文章将介绍Math.NET中实现的其他算法的随机数发生器。请关注

3.资源

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

  如果本文显示有问题,请参考本文原文http://www.cnblogs.com/asxinyu/p/4301544.html




本文转自数据之巅博客园博客,原文链接:http://www.cnblogs.com/asxinyu/p/Dotnet_Opensource_MathNet_Radom_Extend_12.htmlX,如需转载请自行联系原作者
相关文章
|
11月前
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
460 0
|
开发框架 搜索推荐 算法
一个包含了 50+ C#/.NET编程技巧实战练习教程
一个包含了 50+ C#/.NET编程技巧实战练习教程
458 18
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
588 12
|
Linux C# iOS开发
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
384 12
|
开发框架 人工智能 .NET
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
235 6
|
开发框架 算法 .NET
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
254 6
|
开发框架 Cloud Native .NET
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
275 6
|
算法 Java 测试技术
Benchmark.NET:让 C# 测试程序性能变得既酷又简单
Benchmark.NET是一款专为 .NET 平台设计的性能基准测试框架,它可以帮助你测量代码的执行时间、内存使用情况等性能指标。它就像是你代码的 "健身教练",帮助你找到瓶颈,优化性能,让你的应用跑得更快、更稳!希望这个小教程能让你在追求高性能的路上越走越远,享受编程带来的无限乐趣!
896 13
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
949 5
|
机器学习/深度学习 人工智能 Cloud Native
在数字化时代,.NET 技术凭借其跨平台兼容性、丰富的类库和工具集以及卓越的性能与效率,成为软件开发的重要平台
在数字化时代,.NET 技术凭借其跨平台兼容性、丰富的类库和工具集以及卓越的性能与效率,成为软件开发的重要平台。本文深入解析 .NET 的核心优势,探讨其在企业级应用、Web 开发及移动应用等领域的应用案例,并展望未来在人工智能、云原生等方面的发展趋势。
344 3