public class DrawOutTheme
        {
            public Random Randomizer;
            public int Value;
            public TextBox TextBox = null;            public void RandomValue()
            {
                Randomizer = new Random();
                Value = Randomizer.Next(1, 100);
                TextBox.Text = Convert.ToString(Value);
            }
        }private void button1_Click(object sender, EventArgs e)
        {
            DrawOutTheme[] DrawOut = new DrawOutTheme[2];
            DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
            DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
            DrawOut[0].RandomValue();
            DrawOut[1].RandomValue();
        }当按buttion1时 两个textbox的随机是相同的 怎么按buttion时 是两个textbox的随机数是不相同的 求大神帮助

解决方案 »

  1.   

    private void button1_Click(object sender, EventArgs e)
            {
                int a = RandomValue();
                int b = RandomValue();            while (a == b)
                {
                    b = RandomValue();
                }            textBox1.Text = Convert.ToString(a);
                textBox2.Text = Convert.ToString(b);
            }        public int RandomValue()
            {            
                return new Random().Next(1, 100);            
            }
      

  2.   

    之所以和一样,是因为你是同时创建DrawOutTheme,而且是同时调用RandomValue方法
    在中间加一句Thread.Sleep(100);但这样还是会出现一样的,只是几率下小一点            DrawOutTheme[] DrawOut = new DrawOutTheme[2];
                DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
                DrawOut[0].RandomValue();
                Thread.Sleep(100);
                DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
                DrawOut[1].RandomValue();
      

  3.   

    public class DrawOutTheme
      {
      public Random Randomizer=new Random();
      public int Value;
      public TextBox TextBox = null;
      
      public void RandomValue()
      {
      Value = Randomizer.Next(1, 100);
      TextBox.Text = Convert.ToString(Value);
      }
      }private void button1_Click(object sender, EventArgs e)
      {
      DrawOutTheme[] DrawOut = new DrawOutTheme[2];
      DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
      DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
      DrawOut[0].RandomValue();
      DrawOut[1].RandomValue();
      }
     将 Randomizer = new Random()这一句话放到 public Random Randomizer=new Random()即可
      

  4.   

    随机数的种子默认情况下是取当前时间的,因为创建两个Random的时间几乎相同,所以产生的随机数也相同。有两个方法可以解决这个问题:
    1、用Random(int)这个构造函数为每个DrawOutTheme实例指定一个不同的值,比如可以利用Guid的随机性。
      Randomizer = new Random(Guid.NewGuid().GetHashCode())
    2、所有DrawOutTheme实例共享一个Random对象,也就是将Randomizer提升为类的静态成员,然后在类的构造函数里取消对其赋值的操作。
      static Randomizer = new Random()
      

  5.   

    改成这样就可以了
    public class DrawOutTheme
      {
      public Random Randomizer;
      public int Value;
      public TextBox TextBox = null;
       
      public void RandomValue()
      {
      Value = Randomizer.Next(1, 100);
      TextBox.Text = Convert.ToString(Value);
      }
      }private void button1_Click(object sender, EventArgs e)
      {
      Randomizer=new Random();
      DrawOutTheme[] DrawOut = new DrawOutTheme[2];
      DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
      DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
      DrawOut[0].RandomValue();
      DrawOut[1].RandomValue();
      }