2016-03-08 49 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); 
    //... 
}