C#中有两种类型的随机数生成器:

  • 伪随机数(System.Random)
  • 安全随机数(System.Security.Cryptography.RNGCryptoServiceProvider)

   关键的区别在于用于进行随机化的种子值可能不会快速且随机地变化。例如,System.Random依赖于计算机系统时钟

 public static class IntNumRandom {

  /// <summary>   

  /// 生成小于输入值绝对值的随机数   

  /// </summary>   

  /// <param name="NumSides"></param>   

  /// <returns></returns>   

  public static int Next (this int numSeeds) {

    numSeeds = Math.Abs (numSeeds);

    if (numSeeds <= 1) {

      return 0;

    }

 

    int length = 4;

    if (numSeeds <= byte.MaxValue) {

      length = 1;

    } else if (numSeeds <= short.MaxValue) {

      length = 2;

    }

 

    return Next (numSeeds, length);

  }

 

  private static int Next (int numSeeds, int length) {

    // Create a byte array to hold the random value.   

    byte[] buffer = new byte[length];

    // Create a new instance of the RNGCryptoServiceProvider.   

    System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider ();

    // Fill the array with a random value.   

    Gen.GetBytes (buffer);

    // Convert the byte to an uint value to make the modulus operation easier.   

    uint randomResult = 0x0; //这里用uint作为生成的随机数   

    for (int i = 0; i < length; i++) {

      randomResult |= ((uint) buffer[i] << ((length - 1 - i) * 8));

    }

    // Return the random number mod the number   

    // of sides.  The possible values are zero-based   

    return (int) (randomResult % numSeeds);

  }

 

}

 

public class RandomGenerator {

  readonly RNGCryptoServiceProvider csp;

 

  public RandomGenerator () {

    csp = new RNGCryptoServiceProvider ();

  }

 

  public int Next (int minValue, int maxExclusiveValue) {

    if (minValue >= maxExclusiveValue)

      throw new ArgumentOutOfRangeException ("minValue must be lower than maxExclusiveValue");

 

    long diff = (long) maxExclusiveValue - minValue;

    long upperBound = uint.MaxValue / diff * diff;

 

    uint ui;

    do {

      ui = GetRandomUInt ();

    } while (ui >= upperBound);

    return (int) (minValue + (ui % diff));

  }

 

  public uint GetRandomUInt () {

    var randomBytes = GenerateRandomBytes (sizeof (uint));

    return BitConverter.ToUInt32 (randomBytes, 0);

  }

 

  private byte[] GenerateRandomBytes (int bytesNumber) {

    byte[] buffer = new byte[bytesNumber];

    csp.GetBytes (buffer);

    return buffer;

  }

}

 


 

最新文章

  1. 引用log4j.jar包后,出现告警
  2. 高性能的分布式服务框架 Dubbo
  3. ASP FORM表单提交判断
  4. visual studio 2012 has stopped working
  5. iOS 摇一摇
  6. ubuntu12.04进入单用户模式
  7. bzoj1563
  8. Tasklist 命令的使用
  9. 全国三级城市联动 js版
  10. [LeetCode]题解(python):123-Best Time to Buy and Sell Stock III
  11. YARN &amp; HDFS2 安装和配置Kerberos
  12. Nginx + Apache 反向代理
  13. 零基础HTML编码学习笔记
  14. python-day2 字典
  15. EF vs ADO.NET
  16. Gym 100952I&amp;&amp;2015 HIAST Collegiate Programming Contest I. Mancala【模拟】
  17. 手工编程:hello world
  18. collectd+influxDB+Grafana搭建性能监控平台
  19. SpringMVC中 -- @RequestMapping的作用及用法
  20. 在用node安装某个全局模块的时候,没有权限修改node_modules

热门文章

  1. 两种库解析、构造 JSON
  2. oo第二次博客总结
  3. Cocos Creater 监听程序到后台和重新到前台
  4. MATLAB GUI设计(线性卷积和循环卷积的比较--笔记)
  5. PHP多维数组替换某一元素的值
  6. vue-cli@2的原理解析
  7. Git 教程(四):标签和其他
  8. OpenCV学习笔记(二) - 写入视频、jpg格式
  9. Verify_Execute 验证SQL语句执行结果
  10. 微信小程序数组对象