How “good” is this method for generating random numbers?

后端 未结 3 2191
别那么骄傲
别那么骄傲 2021-02-11 08:39

I was searching on google for RNGCryptoServiceProvider with examples on how to limit the range between Max and Min, and still get an even distribution. Before I used modulo oper

3条回答
  •  花落未央
    2021-02-11 09:17

    There is no point in using an encryption class random generator to seed a regular random generator. (By the principle of the weakest link...) Just use a single instance of the random generator, and reuse it:

    private static Random rnd = new Random();
    
    public static int GetRandom(int min, int max) {
      return rnd.Next(min, max);
    }
    

提交回复
热议问题