隨機數產生器是 C# 中的內建函式庫,可隨機產生整數和浮點數。每次呼叫庫的相關方法時,它都會傳回一個隨機數。一系列隨機數字是一組不遵循任何模式的數字。 C# 中的隨機數產生器在每次呼叫時都會產生這樣的序列。
我想到的下一個問題是為什麼他們稱之為偽隨機數產生器類別?讓我們透過現實生活中的人類行為來理解這一點。當一個人被要求選擇隨機顏色時,他會選擇某種顏色。假設他選擇了黃色。是什麼讓他選擇了黃色?這可能是他最喜歡的顏色或周圍環境的顏色,或者他當時可能正在考慮一些黃色的東西。這種驅動隨機選擇某物的決定的人類行為被稱為隨機世界中的種子。種子是隨機性的觸發器或起點。
現在,當種子是可預測的時,隨機數變得不那麼隨機。然後它們被稱為偽隨機數。當不可預測時,它們被稱為安全隨機數。
C# Random Class 使用當前時間戳作為種子,這是非常可預測的。因此,術語偽隨機數產生器類別。
System.Security.Cryptography 命名空間中的 RNGCryptoServiceProvider 類別能夠產生安全隨機數,這些隨機數可用作密碼。
在C#中產生隨機數的第一件事是初始化Random類別。這可以透過類別的兩個建構函式中的任何一個來完成:
讓我們來看一個如何產生隨機整數的範例:
下面的範例產生隨機 Int32 數字。
代碼:
using System; public class Program { public static void Main() { Random rnd = new Random(); for (int i = 0; i < 10; i++) Console.WriteLine("Random number {0} : {1}", i + 1, GenerateRandomInt(rnd)); } public static int GenerateRandomInt(Random rnd) { return rnd.Next(); } }
輸出:
下面的範例產生 0 到 100 範圍內的隨機 Int32 數字。
代碼:
using System; public class Program { public static void Main() { Random rnd = new Random(); for (int i = 0; i < 10; i++) Console.WriteLine("Random number {0} : {1}", i + 1, GenerateRandomInt(rnd)); } public static int GenerateRandomInt(Random rnd) { return rnd.Next(100); } }
輸出:
下面的範例產生 50 到 100 範圍內的隨機 Int32 數字。
代碼:
using System; public class Program { public static void Main() { Random rnd = new Random(); for (int i = 0; i < 10; i++) Console.WriteLine("Random number {0} : {1}", i + 1, GenerateRandomInt(rnd)); } public static int GenerateRandomInt(Random rnd) { return rnd.Next(50, 100); } }
輸出:
讓我們來看一個如何產生隨機浮點數的範例:
下面的範例產生隨機 Int32 數字。
代碼:
using System; public class Program { public static void Main() { Random rnd = new Random(); for (int i = 0; i < 10; i++) Console.WriteLine("Random number {0} : {1}", i + 1, GenerateRandomInt(rnd)); } public static double GenerateRandomInt(Random rnd) { return rnd.NextDouble(); } }
輸出:
The most common mistake developers commit while generating random numbers is that for each random number, they create a new object of Random Class. As illustrated in the example below:
Code:
using System; public class Program { public static void Main() { for (int i = 0; i < 10; i++) Console.WriteLine("Random number {0} : {1}", i + 1, GenerateRandomInt()); } public static int GenerateRandomInt() { Random rnd = new Random(); //a very common mistake return rnd.Next(); } }
Output:
As explained in the working of Random Class, the numbers generated are based on the seed value and the current state of the machine. Any instance of Random class starts with the seed value, saves the current state and uses it to generate the next random number. In the code above, the mistake was to create a new instance of the Random class in every iteration of the loop. So, before the time in the internal clock changes, the code is fully executed, and each instance of Random class is instantiated with the same seed value. This results in the same set of numbers generated every time.
In this article, we learnt about the random number generator in C# and how it internally works to generate random numbers. We also briefly learnt the concept of pseudo-random and secure-random numbers. This information is sufficient for developers to use the Random class in their applications. Deep dive, if interested to explore more on random numbers for passwords and one-time passwords.
以上是C# 中的隨機數產生器的詳細內容。更多資訊請關注PHP中文網其他相關文章!