Random int没有足够快地更新用于创建目的随机按钮

时间:2016-03-08 02:51:13

标签: c# random

我正在做的是生成一个充满随机按钮的5x5网格。问题是,我使用的随机int不会频繁更新以使其真正随机,它将95%的时间将整个网格显示为一个按钮类型,并且在5%的时间内将两个按钮分开。我需要随机选择每个细胞。这是我的代码

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

3 个答案:

答案 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);

答案 1 :(得分:1)

通过使用行Random rnd = new Random();,您正在创建Random类的对象。如果在循环中定义它,每次迭代都会创建一个新对象,因此在Loop外定义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位有符号整数   小于或等于minValue且小于maxValue;也就是说,范围   返回值包括minValue但不包括maxValue。如果是minValue   等于maxValue,返回minValue。

答案 2 :(得分:0)

每次循环时,您都在初始化并播种新的PRNG。 System.Random()是确定性PRNG,默认构造函数使用基于时间的值对其进行种子化。由于系统时钟在重新种子之间没有足够的进展,因此最终会得到相同的值。

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

  

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

您需要创建一个实例并重复使用它:

Random rnd = new Random();

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