-1

I am writing an application in C# for a class project and having troubles writing out the results. It is supposed to simulate rolling a pair of dice 100 times and writing out the results to a sequential file. When I run the code, the loop writes out only one result (i.e. 5,2) for all 100 records. Any ideas on why? When I place a break point at the write statement, it shows the values have changed, but nothing seems to be writing.

        private void button1_Click(object sender, EventArgs e)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\temp\dicerolls.txt", false))


        for (int i = 0; i < 100; i++)
        {
            int dice1 = 0;
            int dice2 = 0;
            Random rnd = new Random();
            dice1 = rnd.Next(1, 7);
            dice2 = rnd.Next(1, 7);
            int result = dice1 + dice2;
            string display = Convert.ToString(result);
            string die1 = Convert.ToString(dice1);
            string die2 = Convert.ToString(dice2);
            lblDie1.Text = "Die 1 = " + die1;
            lblDie2.Text = "Die 2 = " + die2;
            lblResult.Text = "Total = " + display;

            file.WriteLine(Convert.ToString(dice1) + ","+Convert.ToString(dice2)+"\n");

        }
    }

1 Answers1

2

You need to move the Random object out of the scope of the loop.

When you create the random object, it creates a seed at which the stream of pseudo random numbers are generated from. By placing the random number generator outside of the loop, it is able to progress through the random number stream. The stream progresses to the next random number through the invocation of Next. By instantiating inside of the loop, you are essentially resetting the random process.

For example:

Random rnd = new Random();
for (int i = 0; i < 100; i++)
    {
        int dice1 = 0;
        int dice2 = 0;

        dice1 = rnd.Next(1, 7);
        dice2 = rnd.Next(1, 7);
        int result = dice1 + dice2;
        string display = Convert.ToString(result);
        string die1 = Convert.ToString(dice1);
        string die2 = Convert.ToString(dice2);
        lblDie1.Text = "Die 1 = " + die1;
        lblDie2.Text = "Die 2 = " + die2;
        lblResult.Text = "Total = " + display;

        file.WriteLine(Convert.ToString(dice1) + ","+Convert.ToString(dice2)+"\n");

    }
wbennett
  • 2,545
  • 21
  • 12
  • Thank you so much wbennett, when I walk through this in my head, I still can't figure out why your answer is correct? I don't see how recreating a random object did that. Can you point me in the right direction? – user2554353 Jul 07 '14 at 18:55
  • When you create the random object, it creates a seed at which the stream of pseudo random numbers are generated from. By placing the random number generator outside of the loop, it is able to progress through the random number stream, because it doesn't start over. You were essentially resetting the process in each iteration of the loop. – wbennett Jul 07 '14 at 18:58
  • 1
    @user2554353 Please [RTFM](http://msdn.microsoft.com/en-us/library/h343ddh9(v=vs.110).aspx) and the question your is a duplicate of. You should find a very detailed explanation in both places. – tnw Jul 07 '14 at 18:59
  • Thanks again wbennett, I apologize for the duplicate q. It is hard to find things when you are still not sure what to search for. – user2554353 Jul 07 '14 at 19:07