2016-03-08 50 views
-1

我在做什么是生成一个5x5网格填充随机按钮。问题是,我正在使用的随机int没有足够频繁地更新以使其成为真正的随机数,它将95%的时间显示为一个按钮类型的整个网格,并在5%的时间内在2个按钮之间进行分割。我需要随机选择每个细胞。这里是我的代码随机int没有足够快速更新的目的随机按钮创建

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); 
      } 
     } 
    } 

下面是一些如何填充网格的照片。

http://imgur.com/a/jGwL2

+0

对不起我搜索,但没有找到任何重复。如果需要,我可以删除问题。 – kalenpw

+0

你可以,但你不必。它只会帮助将来访问者引导到其他有用的线程。 –

回答

1

不要创建随机class多次:

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); 
+0

没错,谢谢! – kalenpw

+0

@kalenpw你知道新的随机使用当前时间作为它的种子值吗?;)只要种子值没有改变(因为两个实例创建的时间差很小),那么它总是会生成相同的模式,因此你得到了重复的数据。 – Ian

1

使用Random rnd = new Random();你行创建一个的对象类。如果在循环中定义它,每次迭代都会创建一个新对象,因此在循环外部定义Random;并拨打rnd.Next(LowerBound,upperBound);获取下一个随机数;

Random rnd = new Random(); 
for (int i = 0; i < 25; i++) { 
int randomPotion = rnd.Next(0, 4); 
// Iterating content 
} 

注:Random.Next(Int32, Int32):一个32位的比包括maxValue符号整数大于 或等于minValue(最小值)和更小;也就是返回值的范围 包含minValue但不包含maxValue。如果minValue 等于maxValue,则返回minValue。

0

您正在初始化并在每次循环播种一个新的PRNG。 System.Random()是一个确定性PRNG,默认构造函数用基于时间的值对它进行种子处理。由于系统时钟在重置之间没有足够的前进,所以您将以相同的值结束。

the documentation of System.Random..ctor()来自:

默认种子值被从系统时钟导出,并且具有有限的分辨率。因此,通过对默认构造函数的调用而紧密连续创建的不同Random对象将具有相同的默认种子值,因此将生成相同的随机数集合。通过使用单个随机对象来生成所有随机数,可以避免此问题。您还可以通过修改由系统时钟返回的种子值来解决此问题,然后将此新种子值明确提供给Random(Int32)构造函数。有关更多信息,请参阅Random(Int32)构造函数。

你需要创建一个例如和重用:

Random rnd = new Random(); 

for (int i = 0; i < 25; i++) 
{ 
    //... 
    int randomPotion = rnd.Next(0, 4); 
    //... 
}