C# 生成小于Int数值绝对值的随机数
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;
}
}
C# 生成小于Int数值绝对值的随机数的更多相关文章
- js中Math.random()生成指定范围数值的随机数
http://www.111cn.net/wy/js-ajax/57062.htm Math.random() 这个方法相信大家都知道,是用来生成随机数的.不过一般的参考手册时却没有说明如何用这个方法 ...
- JS对象随机数 random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。 注意:返回一个大于或等于 0但小于1的符号为正的数值
随机数 random() random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数. 语法: Math.random(); 注意:返回一个大于或等于 0 但小于 1 ...
- 生成N个不相等的随机数
近期项目中须要生成N个不相等的随机数.实现的时候.赶工期,又有项目中N非常小(0-100)直接谢了一个最直观的方法: public static List<Integer> randomS ...
- PHP函数:生成N个不重复的随机数
思路:将生成的随机数存入数组,再在数组中去除重复的值,即可生成一定数量的不重复随机数. 程序: <?php /* * array unique_rand( int $min, int $max, ...
- 生成N个不重复的随机数(转)
有25幅作品拿去投票,一次投票需要选16幅,单个作品一次投票只能选择一次.前面有个程序员捅了漏子,忘了把投票入库,有200个用户产生的投票序列为空.那么你会如何填补这个漏子? 当然向上级反映情况.但是 ...
- PHP CodeBase: 生成N个不重复的随机数
有25幅作品拿去投票,一次投票需要选16幅,单个作品一次投票只能选择一次.前面有个程序员捅了漏子,忘了把投票入库,有200个用户产生的投票序列为空.那么你会如何填补这个漏子? <?php /* ...
- 使用C++生成1-33中的6个随机数,无重复
生成1-33中的6个随机数,无重复 ------------------------------------------------------------------------ 方法1.每生成 ...
- php生成N个不重复的随机数实例
思路: 将随机数存入数组,再在数组中去除重复的值,即可生成一定数量的不重复随机数. /* * array unique_rand( int $min, int $max, int $num ) * 生 ...
- 函数:生成n个互不相同的随机数,最大值为upper
参考:http://blog.csdn.net/zhangkaihang/article/details/6836506 函数getRandArray()功能说明: 入参:int upper-生成的随 ...
随机推荐
- filter与map函数
·filter()函数filter()函数包括两个参数,分别是function和list.该函数根据function参数返回的结果是否为真来过滤list参数中的项,最后返回一个新列表,如下例所示:&g ...
- ELK日志分析解决方案
概要 ELK(Elasticsearch , Logstash, Kibana的简称)是目前比较流行的日志分析解决方案,核心包括了三个部分 Elasticsearch:日志查询分析引擎 Logstas ...
- Java基础(面试题)
1:面向对象编程有很多重要的特性: 封装,继承,多态和抽象. 2:什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? (1)Java虚拟机是一个可以执行Java字节码的虚拟机进程.J ...
- vue中使用vue-quill-editor及上传图片到自己服务器
第一步,下载依赖 cnpm install vue-quill-editor --save 第二步,再main.js里引入组件(我这里是全局注册) // 富文本编辑器 import VueQuillE ...
- php读取和导出Excel文件
require 'vendor/PHPExcel/PHPExcel.php';require 'vendor/PHPExcel/PHPExcel/IOFactory.php'; public func ...
- jquery.uploadify上传插件HTML5版中文api使用说明
插件官网文档:http://www.uploadify.com/documentation/ H5版下载地址:https://download.csdn.net/download/u010075697 ...
- css基础重点内容总结
一.目录引入 ./同级(当前) ../上级目录 ../../上上级目录 二.标签种类: 1.块级标签(block):独占一行,宽高可设: 2.行内块标签(inline-block):不独占一行,宽高 ...
- Lists.newArrayListWithExpectedSize( int estimatedSize)
Lists.newArrayListWithExpectedSize( int estimatedSize) 构造一个期望长度为estimatedSize的ArrayList实例. 源码: publ ...
- centos7.4卸载再安装mariadb服务无法启动问题
今天yum安装MariaDB完成后,启动服务时一直报以下错误 Job for mariadb.service failed. See ‘systemctl status mariadb.service ...
- java面向对象总结(二)
Java 封装 实现Java封装的步骤 java面向对象值继承 概念: 继承的格式: 类和类之间的关系: 继承的特点: 继承的优缺点 继承的好处: 继承的缺点: 继承的注意事项: 使用继承的步骤: J ...