C# 随机int更新速度不够快,无法创建随机按钮
我正在做的是生成一个5x5网格,其中填充了随机按钮。问题是,我使用的随机整数更新频率不足以使其成为真正的随机整数,它将在95%的时间内以一个按钮类型显示整个网格,并在5%的时间内在两个按钮之间分割。我需要随机选择每个单元格。这是我的密码C# 随机int更新速度不够快,无法创建随机按钮,c#,random,C#,Random,我正在做的是生成一个5x5网格,其中填充了随机按钮。问题是,我使用的随机整数更新频率不足以使其成为真正的随机整数,它将在95%的时间内以一个按钮类型显示整个网格,并在5%的时间内在两个按钮之间分割。我需要随机选择每个单元格。这是我的密码 private void btnFillGrid_Click(object sender, RoutedEventArgs e) { stkGameBoardColumnOne.Children.Clear(); stkGameB
private void btnFillGrid_Click(object sender, RoutedEventArgs e) {
stkGameBoardColumnOne.Children.Clear();
stkGameBoardColumnTwo.Children.Clear();
stkGameBoardColumnThree.Children.Clear();
stkGameBoardColumnFour.Children.Clear();
stkGameBoardColumnFive.Children.Clear();
for (int i = 0; i < 25; i++) {
Random rnd = new Random();
Button btnMapCell = new Button();
Potion cellPotion = new Potion("", 0, "");
btnMapCell.Foreground = new SolidColorBrush(Colors.White);
int randomPotion = rnd.Next(0, 4);
if (randomPotion == 0) {
//Potion cellPotion = new Potion("Small Healing Potion", 25, "green");
cellPotion.Name = "Small Healing Potion";
cellPotion.AffectValue = 25;
cellPotion.Color = "green";
btnMapCell.Background = new SolidColorBrush(Colors.Green);
}
else if (randomPotion == 1) {
//Potion cellPotion = new Potion("Medium Healing Potion", 50, "blue");
cellPotion.Name = "Medium Healing Potion";
cellPotion.AffectValue = 50;
cellPotion.Color = "blue";
btnMapCell.Background = new SolidColorBrush(Colors.Blue);
}
else if (randomPotion == 2) {
//Potion cellPotion = new Potion("Large Healing Potion", 100, "purple");
cellPotion.Name = "Large Healing Potion";
cellPotion.AffectValue = 100;
cellPotion.Color = "purple";
btnMapCell.Background = new SolidColorBrush(Colors.Purple);
}
else if (randomPotion == 3) {
//Potion cellPotion = new Potion("Extreme Healing Potion", 200, "red");
cellPotion.Name = "Extreme Healing Potion";
cellPotion.AffectValue = 200;
cellPotion.Color = "red";
btnMapCell.Background = new SolidColorBrush(Colors.Red);
}
btnMapCell.Content = "";
btnMapCell.Width = 75;
btnMapCell.Height = 75;
btnMapCell.Content = cellPotion.Name + "\r\n" + "(" + cellPotion.AffectValue + ")";
if (i >= 0 && i < 5) {
stkGameBoardColumnOne.Children.Add(btnMapCell);
}
else if (i >= 5 && i < 10) {
stkGameBoardColumnTwo.Children.Add(btnMapCell);
}
else if (i >= 10 && i < 15) {
stkGameBoardColumnThree.Children.Add(btnMapCell);
}
else if (i >= 15 && i < 20) {
stkGameBoardColumnFour.Children.Add(btnMapCell);
}
else if (i >= 20 && i < 25) {
stkGameBoardColumnFive.Children.Add(btnMapCell);
}
}
}
下面是它如何填充网格的一些图片
不要多次创建随机类:
for (int i = 0; i < 25; i++) {
Random rnd = new Random(); //don't do this
Random rnd = new Random(); //declare this once, somewhere...
//and use it multiple times:
for (int i = 0; i < 25; i++) {
int newrand = rnd.Next(0, upperlimit);
但要多次使用它:
for (int i = 0; i < 25; i++) {
Random rnd = new Random(); //don't do this
Random rnd = new Random(); //declare this once, somewhere...
//and use it multiple times:
for (int i = 0; i < 25; i++) {
int newrand = rnd.Next(0, upperlimit);
通过使用线随机rnd=新随机;您正在创建类的对象。如果在循环中定义它,每个迭代将创建一个新对象,因此在循环外定义Random;并调用rnd.NextLowerBound,上限;获取下一个随机数
Random rnd = new Random();
for (int i = 0; i < 25; i++) {
int randomPotion = rnd.Next(0, 4);
// Iterating content
}
注意:32位有符号整数大于
小于或等于minValue且小于maxValue;就是范围
返回值的类型包括minValue,但不包括maxValue。如果最小值
等于maxValue,则返回minValue
每次通过循环,您都在初始化和播种一个新的PRNG。随机是一种确定性PRNG,默认构造函数使用基于时间的值对其进行种子设定。由于系统时钟在重新设定种子之间的前进速度不够快,因此将以相同的值结束 发件人: 默认种子值源自系统时钟,具有有限分辨率。因此,通过调用默认构造函数连续创建的不同随机对象将具有相同的默认种子值,因此将生成相同的随机数集。通过使用单个随机对象生成所有随机数,可以避免此问题。您还可以通过修改系统时钟返回的种子值,然后显式地将此新种子值提供给RandomInt32构造函数来解决此问题。有关更多信息,请参阅RandomInt32构造函数 您需要创建一个实例并重用它:
Random rnd = new Random();
for (int i = 0; i < 25; i++)
{
//...
int randomPotion = rnd.Next(0, 4);
//...
}
是的,这就成功了,谢谢@kalenpw你知道新的Random使用当前时间作为其种子值吗;只要种子值不会因为创建两个实例之间的时间差很小而改变,那么它将始终生成相同的模式,因此您得到了重复项..很抱歉,我搜索了重复项,但找不到任何内容。如果需要,我可以删除这个问题。