Correct method of a “static” Random.Next in C#?

前端 未结 11 2234
执念已碎
执念已碎 2020-12-10 10:51

Why do i need to create an instance of Random class, if i want to create a random number between 1 and 100 ....like

Random rand = new Random();
rand.Next(1,1         


        
11条回答
  •  误落风尘
    2020-12-10 11:27

    //Function to get random number
    private static readonly Random random = new Random();
    private static readonly object syncLock = new object();
    public static int RandomNumber(int min, int max)
    {
        lock(syncLock) { // synchronize
            return random.Next(min, max);
        }
    }
    

    Copied directly from

提交回复
热议问题