-2

This is my code. I cant understand why the n variable doesn't change when the loop loops. Right now it just writes the first random letter and do it ten times instead of making it random every loop.

            string[] alfa = new string[16]{
                "a", "b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"
            };

            using (var tw = new StreamWriter(@"../../data/" + 
             NewUsername.Text + "," + NewPassword.Text + "/ReFectorPassword.txt", true))
            {

                for (var i = 0; i < 10; i++)
                {

                    Random r = new Random();
                    int n = r.Next(16);

                    randomString = alfa[n];
                    tw.Write(randomString );
                }

            }
Spy
  • 17
  • 4

1 Answers1

-1

One trick is to manually create the seed by adding DateTime.Now.Ticks to the variable i:

Random r = new Random((int)DateTime.Now.Ticks + i);
Amir Molaei
  • 3,700
  • 1
  • 17
  • 20