随机数生成器是 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中文网其他相关文章!