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

前端 未结 11 2214
执念已碎
执念已碎 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

    You need something similar to this if you want the syntax you mention.

    namespace MyRandom
    {
        public class Random
        {
            private static m_rand = new Random();
            public static Next(int min, int max)
            {
                return m_rand.Next(min, max);
            }
        }
    }
    

    This should allow you to do Random.Next(1,100); without having to worry about seeding.

提交回复
热议问题