如何在C#中生成随机整数?


当前回答

如其他答案中所述,一个好的安全方法是使用安全密码生成器。这里的所有示例都显示了RNGCryptoServiceProvider的用法,与我建议的解决方案相比,它编写的代码很长。

使用RandomNumberGenerator,它是在密码学API之上编写的。它和RNGCryptoServiceProvider一样安全,并且具有相同的随机性。

// Gives a random number for the integer range.
// You can simply update the parameters as your needs.
RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);

其他回答

内置Random类(System.Random)生成的数字生成伪随机数。

如果你想要真正的随机数,我们最接近的就是“安全伪随机生成器”,它可以通过使用C#中的Cryptographic类(如RNGCryptoServiceProvider)生成。

即使如此,如果你仍然需要真正的随机数,你将需要使用一个外部源,比如解释放射性衰变的设备,作为随机数发生器的种子。因为,根据定义,任何纯算法产生的数字都不可能是真正随机的。

Random类用于创建随机数。(当然是伪随机)。

例子:

Random rnd = new Random();
int month  = rnd.Next(1, 13);  // creates a number between 1 and 12
int dice   = rnd.Next(1, 7);   // creates a number between 1 and 6
int card   = rnd.Next(52);     // creates a number between 0 and 51

如果您要创建多个随机数,则应保留random实例并重复使用。如果您创建的新实例时间太近,它们将生成与随机生成器从系统时钟中生成的序列相同的随机数。

您可以在他为伪随机数构建的MiscUtil类库中使用JonSkeet的StaticRandom方法。

using MiscUtil;
...

for (int i = 0; i < 100; 
    Console.WriteLine(StaticRandom.Next());

从这里修改答案。

如果您可以访问与Intel Secure Key兼容的CPU,则可以使用以下库生成真正的随机数和字符串:https://github.com/JebteK/RdRand和https://www.rdrand.com/

只需从这里下载最新版本,包括Jebtek.RdRand并为其添加一个using语句。然后,您需要做的就是:

// Check to see if this is a compatible CPU
bool isAvailable = RdRandom.GeneratorAvailable();

// Generate 10 random characters
string key       = RdRandom.GenerateKey(10);

 // Generate 64 random characters, useful for API keys 
string apiKey    = RdRandom.GenerateAPIKey();

// Generate an array of 10 random bytes
byte[] b         = RdRandom.GenerateBytes(10);

// Generate a random unsigned int
uint i           = RdRandom.GenerateUnsignedInt();

如果您没有兼容的CPU来执行代码,只需使用rdrand.com上的RESTful服务即可。使用项目中包含的RdRandom包装库,您只需要这样做(注册时可以获得1000个免费调用):

string ret = Randomizer.GenerateKey(<length>, "<key>");
uint ret   = Randomizer.GenerateUInt("<key>");
byte[] ret = Randomizer.GenerateBytes(<length>, "<key>");

请注意,新的Random()是在当前时间戳上播种的。

如果您只想生成一个数字,可以使用:

new Random().Next(int.MinValue,int.MaxValue)

有关更多信息,请查看Random类,但请注意:

然而,由于时钟具有有限的分辨率,使用无参数构造器来连续创建不同的随机对象会创建随机数生成器,生成相同的随机数序列

因此,不要使用此代码生成一系列随机数。