1. 程式人生 > >【原創】開源Math.NET基礎數學類庫使用(12)C#隨機數擴充套件方法

【原創】開源Math.NET基礎數學類庫使用(12)C#隨機數擴充套件方法

  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基礎數學使用(02)矩陣向量計算

前言   本文開始一一介紹Math.NET的幾個主要子專案的相關功能的使用。今天先要介紹的是最基本Math.NET Numerics的最基本矩陣與向量計算。 1.建立Numerics矩陣與向量   矩陣與向量計算是數學計算的核心,因此也是Math.NET Numerics的核心和基礎。  

原創開源Math.NET基礎數學使用(08)C#進行數值積分

  在數值計算的需求中,數值積分也是比較常見的一個。我們也知道像Matlab,Mathematics等軟體的積分求解功能非常高大上,不僅能求解定積分,還能求解不定積分,甚至多重積分等等。而Math.NET這個元件沒有如此高階的功能,目前也只提供了比較件的閉區間上的定積分求解功能。今天就一起來看看,因為不定

原創開源Math.NET基礎數學使用(13)C#實現其他隨機數生成器

1 public abstract class RandomSource : System.Random 2 { 3 readonly bool _threadSafe; 4 readonly object _lock = new objec

原創開源Math.NET基礎數學使用(05)C#解析Delimited Formats資料格式

前言   上一篇文章,我們介紹了使用C#讀寫Matlab的Mat資料格式和通用的Matrix Market資料格式。今天還要介紹一個Math.NET讀取Delimited Formats資料格式的例子。Delimited Formats(也稱DSV) 資料格式也是一種比較常見的資料儲存和交換格式,和CS

原創開源Math.NET基礎數學使用(03)C#解析Matlab的mat格式

前言   本人在09年使用該元件的時候,主要原因也是為了替代Matlab,進行相關數學計算,現在依然有很多人關注Matlab計算,特別是學生,而很多也在使用C#,所以這些人通常由於個人能力有限(無法精通某一個門語言來解決綜合問題),無法單純的通過C#或者Matlab來解決問題,就想通過混合程式設計來

原創開源Math.NET基礎數學使用(16)C#計算矩陣秩

  上個月對Math.NET的基本使用進行了介紹,主要內容有矩陣,向量的相關操作,解析資料格式,數值積分,資料統計,相關函式,求解線性方程組以及隨機數發生器的相關內容。這個月接著深入發掘Math.NET的各種功能,並對原始碼進行分析,使得大家可以儘可能的使用Math.NET在.NET平臺下輕易的開發數學計

原創開源Math.NET基礎數學使用(14)C#生成安全的隨機數

  真正意義上的隨機數(或者隨機事件)在某次產生過程中是按照實驗過程中表現的分佈概率隨機產生的,其結果是不可預測的,是不可見的。而計算機中的隨機函式是按照一定演算法模擬產生的,其結果是確定的,是可見的。我們可以這樣認為這個可預見的結果其出現的概率是100%。所以用計算機隨機函式所產生的“隨機數”並不隨機,

原創開源Math.NET基礎數學使用(10)C#進行基本資料統計

1 /// <summary> 2 /// 對未排序的陣列進行統計操作 警告: Methods with the Inplace-suffix may modify the data array by reordering its entries. 3 /// </su

原創開源Math.NET基礎數學使用(12)C#隨機數擴充套件方法

1 /// <summary>這個類是對System.Random類的擴充套件,擴充套件方法可以生成更多型別的偽隨機數,而不是僅僅是double和Int32型別</summary> 2 /// <remarks>這個擴充套件是執行緒安全的,並且只有在Math.

原創開源Math.NET基礎數學使用(09)相關數論函式使用

1 /// <summary> 2 /// 整數數論函式 3 /// Integer number theory functions. 4 /// </summary> 5 public static class Euclid 6 { 7

原創開源Math.NET基礎數學使用(01)綜合介紹

前言   幾年前接觸這個元件的時候,只需要在.NET平臺進行一些常規的微積分計算,功能還比較少,只限於常規的數值計算,現在已經功能越來越多了,應該是目前最好的替代Matlab進行數值計算的.NET元件。本文及接下來的幾篇文章將詳細的對該元件進行介紹。還有在.NET平臺使用相關Matlab混編

原創開源Math.NET基礎數學使用(06)直接求解線性方程組

  在前幾篇關於Math.NET的部落格中(見上面連結),主要是介紹了Math.NET中主要的數值功能,並進行了簡單的矩陣向量計算例子,接著使用Math.NET的矩陣等物件,對3種常用的矩陣資料交換格式的讀寫。一方面可以瞭解Math.NET的使用,另一方面以後也可以直接讀取和儲存資料為這兩種格式,給大家的

原創開源Math.NET基礎數學使用(11)C#計算相關係數

/// <summary>2個數據集的相關度計算類</summary> public static class Correlation { /// <summary>計算皮爾遜積差相關係數</summary> /// <para

原創開源Math.NET基礎數學使用(07)常用的數學物理常數

1 /// <summary>數學常數 e ,也稱 尤拉數(Euler's number)</summary> 2 public const double E = 2.7182818284590452353602874713526624977572470937000d;

原創開源Math.NET基礎數學使用(15)C#計算矩陣行列式

  上個月對Math.NET的基本使用進行了介紹,主要內容有矩陣,向量的相關操作,解析資料格式,數值積分,資料統計,相關函式,求解線性方程組以及隨機數發生器的相關內容。這個月接著深入發掘Math.NET的各種功能,並對原始碼進行分析,使得大家可以儘可能的使用Math.NET在.NET平臺下輕易的開發數

原創開源Math.NET基礎數學使用(04)C#解析Matrix Marke資料格式

前言   上一篇文章,我們介紹了使用C#讀寫Matlab的Mat資料格式的情況。mat格式的廣泛應用使得很多人都瞭解,但同樣還有一些資料格式也是在科學計算,資料分析,測試等方面的通用資料格式,那就是接下來我們要介紹的Matrix Market格式。我們同樣是使用C#來操作該格式。 1.Matr

原創開源Math.NET基礎數學使用(17)C#計算矩陣條件數

  上個月對Math.NET的基本使用進行了介紹,主要內容有矩陣,向量的相關操作,解析資料格式,數值積分,資料統計,相關函式,求解線性方程組以及隨機數發生器的相關內容。這個月接著深入發掘Math.NET的各種功能,並對原始碼進行分析,使得大家可以儘可能的使用Math.NET在.NET平臺下輕易的開發數學計

原創開源.NET排列組合元件KwCombinatorics使用(二)——排列生成

前言   本文今天介紹的.NET開源元件是KwCombinatorics,它是.NET平臺一個高效的生成排列組合序列的開源類庫,它提供了4種生成排列與組合序列的方式。雖然原理和功能都很簡單,但是這個類庫在軟體測試、組合數學以及密碼學等方面都有很大的用處。很早就接觸了這個類庫,以前在一些小程式

原創開源.NET排列組合元件KwCombinatorics使用(三)——笛卡爾積組合

前言   本文今天介紹的.NET開源元件是KwCombinatorics,它是.NET平臺一個高效的生成排列組合序列的開源類庫,它提供了4種生成排列與組合序列的方式。雖然原理和功能都很簡單,但是這個類庫在軟體測試、組合數學以及密碼學等方面都有很大的用處。很早就接觸了這個類庫,以前在一些小

原創開源.NET排列組合元件KwCombinatorics使用(一)—組合生成

1.Combination類基本介紹   Combination類是根據指定的物件列表,依次升序選擇非重複數字的組合序列,重複是什麼意思呢?就是指定序列中的元素不重複選擇2次。舉個例子:從 0,1,2,3這4個數中,取出3個元素組成序列,那麼共有這麼幾種組合方式:{0,1,2},{0,1,3},{0,2