random string generation - two generated one after another give same results

前端 未结 5 665
无人共我
无人共我 2020-12-21 15:35

I have a simple piece of code:

public string GenerateRandomString()
        {
            string randomString = string.Empty;
            Random r = new Rand         


        
5条回答
  •  抹茶落季
    2020-12-21 16:06

    Since the Random generator is tied with the system clock you are probably displaying the same results with that time period. There are several ways to correct. If you are using loops place the Random rnd = new Random(); outside of the loop.

    Place the Random rnd = new Random(); line where you declare your variables and use the same variable throughout your program (rnd for this example).

    This will work in most cases.

提交回复
热议问题