0

I came across a weired issue: When having created a form in C# to assign randomized numbers to textBox-fields I always get the same numbers returned. I.e.

1 1
1 1
1 1
1 1

The crazy thing about it is the following: When debugging it I get different numbers. Here is the source code:

namespace BlaBla
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = homeGame().ToString();
            textBox2.Text = awayGame().ToString();

            textBox3.Text = homeGame().ToString();
            textBox4.Text = awayGame().ToString();

            textBox5.Text = homeGame().ToString();
            textBox6.Text = awayGame().ToString();

            textBox7.Text = homeGame().ToString();
            textBox8.Text = awayGame().ToString();

        }

        private int homeGame()
        {
            int homeGoal = 0;
            Random rdHome = new Random();
            int homeNumber = rdHome.Next(0, 72);

            if (homeNumber < 10)
            {
                homeGoal = 0;
            }
            if (homeNumber > 10 && homeNumber < 36)
            {
                homeGoal = 1;
            }
            if (homeNumber > 35 && homeNumber < 56)
            {
                homeGoal = 2;
            }
            if (homeNumber > 55 && homeNumber < 71)
            {
                homeGoal = 3;
            }
            if (homeNumber > 70)
            {
                homeGoal = 4;
            }
            return homeGoal;
        }

        private int awayGame()
        {
            int awayGoal = 0;
            Random rdAway = new Random();
            int awayNumber = rdAway.Next(0, 700);

            if (awayNumber < 100)
            {
                awayGoal = 0;
            }
            if (awayNumber > 100 && awayNumber < 411)
            {
                awayGoal = 1;
            }
            if (awayNumber > 410 && awayNumber < 506)
            {
                awayGoal = 2;
            }
            if (awayNumber > 505 && awayNumber < 661)
            {
                awayGoal = 3;
            }
            if (awayNumber > 660)
            {
                awayGoal = 4;
            }
            return awayGoal;
        }

    }
}

I know, this code is not the best in the world. However does someone have an idea what is wrong here? Respectively what would be the corrected code please?

Joey
  • 511
  • 6
  • 20
  • Damn right when I had all my answer typed up. Good call on the duplicate. – Sam Marion Sep 20 '17 at 19:54
  • @GrantWinney: Thank you. To be sure, I understand things correctly: Next to public partial class Form1 : Form I am supposed to create another class and hand the outcome of the random variable over to class Form1, right? – Joey Sep 20 '17 at 20:14
  • 1
    @Joey no, just use a class-level field instead of a method-specific instance for that Random. – Hans Kesting Sep 20 '17 at 20:24

0 Answers0