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

前端 未结 5 667
无人共我
无人共我 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:16

    It's because you're creating two random objects at the same time. This is giving it the same seed, so you're going to get the same numbers.

    When you debug it, there's time between the creation of the random objects which allow them to get different seeds.

提交回复
热议问题