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

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

    The default constructor for Random (the one you're using) seeds the generator with a value based on the current time. If the time in milliseconds doesn't change between the first and second call of the function, it would use the same random seed.

    My suggestion is to use a static Random object and only initialize it once.

提交回复
热议问题