Random.Next() sometimes returns same number in separate threads

前端 未结 3 1716
别那么骄傲
别那么骄傲 2020-12-06 17:44

I have the following class

class Program
{
   static Random _Random = new Random();

   static void Main(string[] args)
   {
      ...
      for (int i = 0;          


        
3条回答
  •  误落风尘
    2020-12-06 18:04

    Random is a pseudo-random number generator and there's nothing preventing it from returning same result for multiple calls. After all there's a probability for this happening. Not to mention that according to the documentation:

    Any instance members are not guaranteed to be thread safe.

    So you shouldn't be calling the Next method at all from multiple threads.

提交回复
热议问题