How do I generate a random int number?

前端 未结 30 2628
长发绾君心
长发绾君心 2020-11-21 11:02

How do I generate a random integer in C#?

30条回答
  •  囚心锁ツ
    2020-11-21 11:43

    There are a number utility functions or services that are better cached in the same way that System.Random should be, so it lends itself to a generic implementation:

    static public class CachedService where T : new() {
        static public T Get { get; } = new T();
    }
    

    To use for random (or similar):

    CachedService.Get.Next(999);
    

提交回复
热议问题