首页  >  文章  >  后端开发  >  C# 中的随机数生成器

C# 中的随机数生成器

WBOY
WBOY原创
2024-09-03 15:34:50415浏览

随机数生成器是 C# 中的内置库,可随机生成整数和浮点数。每次调用库的相关方法时,它都会返回一个随机数。一系列随机数是一组不遵循任何模式的数字。 C# 中的随机数生成器在每次调用时都会生成这样的序列。

C# 中的随机类

  • 那么,C# 是如何生成一系列随机数的呢?答案就在 C# 系统命名空间的 Random Class 中。
  • Random 类是一个伪随机数生成器类。这意味着此类的任务是生成一系列不遵循任何模式的数字。但是,机器真的能够生成随机数吗?机器如何知道接下来要生成哪个数字?毕竟,机器被设计为遵循指令并执行算法。
  • 不,机器无法自行生成随机数。有一个基于当前时钟和机器状态的定义的数学算法,指导它从一组中挑选数字。集合中的所有数字被选中的概率相同。因此,它们并不是完全随机的。他们确实遵循某种模式。只是图案的随机性足以满足人类的实际需求。

伪且安全

我想到的下一个问题是为什么他们将其称为伪随机数生成器类?让我们通过现实生活中的人类行为来理解这一点。当一个人被要求选择一种随机颜色时,他会选择某种颜色。假设他选择了黄色。是什么让他选择了黄色?这可能是他最喜欢的颜色或周围环境的颜色,或者他当时可能正在考虑一些黄色的东西。这种驱动随机选择某物的决定的人类行为被称为随机世界中的种子。种子是随机性的触发器或起点。

现在,当种子是可预测的时,随机数变得不那么随机。然后它们被称为伪随机数。当不可预测时,它们被称为安全随机数。

C# Random Class 使用当前时间戳作为种子,这是非常可预测的。因此,术语伪随机数生成器类。

RNGCryptoServiceProvider 类

System.Security.Cryptography 命名空间中的 RNGCryptoServiceProvider 类能够生成安全随机数,这些随机数可用作密码。

C# 中的随机数生成器函数

在C#中生成随机数的第一件事是初始化Random类。这可以通过类的两个构造函数中的任何一个来完成:

  • Random():使用基于时间的种子值初始化 Random 类的对象。种子值是机器的当前时间戳。不过,在后来的版本中,这被更改为基于 GUID。
  • Random(Int32):使用指定的种子值初始化 Random 类的对象。为了从该系列中获取下一个随机数,我们调用 Random 类的 Next() 方法。
  • Next(): 返回一个非负伪随机 Int32 整数。
  • Next(Int32):返回小于指定整数的非负伪随机 Int32 整数。
  • Next(Int32, Int32): 返回指定范围内的非负伪随机 Int32 整数。

C# 中的整数随机数生成器

让我们看一个如何生成随机整数的示例:

示例#1

下面的示例生成随机 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();
}
}

输出:

C# 中的随机数生成器

示例#2

下面的示例生成 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);
}
}

输出:

C# 中的随机数生成器

示例 #3

下面的示例生成 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);
}
}

输出:

C# 中的随机数生成器

在 C# 中生成浮点数

让我们看一个如何生成随机浮点数的示例:

示例#1

下面的示例生成随机 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();
}
}

输出:

C# 中的随机数生成器

A Very Common Mistake

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:

Example #1

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:

C# 中的随机数生成器

How Random Numbers are all the same and Why did this happen?

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.

Conclusion

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn