如何在C#中生成随机整数?
当前回答
如果您希望CSRNG生成最小值和最大值之间的随机数,这是适合您的。它将使用安全的随机种子初始化随机类。
class SecureRandom : Random
{
public static byte[] GetBytes(ulong length)
{
RNGCryptoServiceProvider RNG = new RNGCryptoServiceProvider();
byte[] bytes = new byte[length];
RNG.GetBytes(bytes);
RNG.Dispose();
return bytes;
}
public SecureRandom() : base(BitConverter.ToInt32(GetBytes(4), 0))
{
}
public int GetRandomInt(int min, int max)
{
int treashold = max - min;
if(treashold != Math.Abs(treashold))
{
throw new ArithmeticException("The minimum value can't exceed the maximum value!");
}
if (treashold == 0)
{
throw new ArithmeticException("The minimum value can't be the same as the maximum value!");
}
return min + (Next() % treashold);
}
public static int GetRandomIntStatic(int min, int max)
{
int treashold = max - min;
if (treashold != Math.Abs(treashold))
{
throw new ArithmeticException("The minimum value can't exceed the maximum value!");
}
if(treashold == 0)
{
throw new ArithmeticException("The minimum value can't be the same as the maximum value!");
}
return min + (BitConverter.ToInt32(GetBytes(4), 0) % treashold);
}
}
其他回答
Random rand = new Random();
int name = rand.Next()
在第二个括号中放入所需的值确保通过编写prop和double tab来设置名称以生成代码
除了COBOL答案,我已经尝试了所有这些解决方案。。。英雄联盟
这些解决方案都不够好。我需要一个快速的for int循环中的随机数,即使在很宽的范围内,我也得到了大量的重复值。在接受了太长时间的随机结果之后,我决定最终彻底解决这个问题。
这都是关于种子的。
我通过解析Guid中的非数字来创建一个随机整数,然后使用它来实例化random类。
public int GenerateRandom(int min, int max)
{
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
return new Random(seed).Next(min, max);
}
更新:如果实例化Random类一次,则无需种子化。所以最好创建一个静态类并调用一个方法。
public static class IntUtil
{
private static Random random;
private static void Init()
{
if (random == null) random = new Random();
}
public static int Random(int min, int max)
{
Init();
return random.Next(min, max);
}
}
然后您可以像这样使用静态类。。
for(var i = 0; i < 1000; i++)
{
int randomNumber = IntUtil.Random(1,100);
Console.WriteLine(randomNumber);
}
我承认我更喜欢这种方法。
这个问题看起来很简单,但答案有点复杂。如果你看到几乎所有人都建议使用Random类,有些人建议使用RNG加密类。但是,什么时候选择。
为此,我们需要首先理解随机性这一术语及其背后的哲学。
我鼓励大家观看我制作的这段视频,它使用C#深入探讨了随机性的哲学https://www.youtube.com/watch?v=tCYxc-2-3年
首先,让我们了解随机性的哲学。当我们告诉一个人在红色、绿色和黄色之间选择时,内部会发生什么。是什么让一个人选择红色、黄色或绿色?
一些最初的想法进入了人们的头脑,决定了他的选择,它可以是最喜欢的颜色,幸运的颜色等等。换句话说,一些最初的触发,我们在RANDOM中称之为SEED。这个SEED是起点,是促使他选择RANDOM值的触发。
现在,如果种子很容易猜测,那么这些随机数被称为伪随机数,而当种子很难猜测时,这些随机数则被称为安全随机数。
例如,一个人根据天气和声音组合选择颜色,那么很难猜出最初的种子。
现在让我发表一个重要声明:-
*“Random”类只生成PSEUDO随机数,要生成SECURE随机数,我们需要使用“RNGCryptoServiceProvider”类。
随机类从CPU时钟中获取种子值,这是非常可预测的。换句话说,C#的RANDOM类生成伪随机数,下面是相同的代码。
Random random = new Random();
int randomNumber = random.Next();
而RNGCryptoServiceProvider类使用OS熵生成种子。OS熵是一个随机值,使用声音、鼠标点击、键盘计时、温度等生成。下面是相同的代码。
using (RNGCryptoServiceProvider rg = new RNGCryptoServiceProvider())
{
byte[] rno = new byte[5];
rg.GetBytes(rno);
int randomvalue = BitConverter.ToInt32(rno, 0);
}
要了解操作系统熵,请看我的视频,从14:30开始https://www.youtube.com/watch?v=tCYxc-2-3fY,其中解释了OS熵的逻辑。所以用简单的词RNG Crypto生成SECURE随机数。
您可以在他为伪随机数构建的MiscUtil类库中使用JonSkeet的StaticRandom方法。
using MiscUtil;
...
for (int i = 0; i < 100;
Console.WriteLine(StaticRandom.Next());
尝试以下简单步骤创建随机数:
创建函数:
private int randomnumber(int min, int max)
{
Random rnum = new Random();
return rnum.Next(min, max);
}
在要使用随机数的位置使用上述函数。假设您想在文本框中使用它。
textBox1.Text = randomnumber(0, 999).ToString();
0是最小值,999是最大值。您可以将值更改为任意值。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 如何在Ruby中生成a和b之间的随机数?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?