当前位置:网站首页>C#的随机数生成
C#的随机数生成
2022-04-23 17:54:00 【IT技术猿猴】
随机数是专门的随机试验的结果。在统计学的不同技术中需要使用随机数,比如在从统计总体中抽取有代表性的样本的时候,或者在将实验动物分配到不同的试验组的过程中,或者在进行蒙特卡罗模拟法计算的时候等等。 产生随机数有多种不同的方法。这些方法被称为随机数发生器。随机数最重要的特性是:它所产生的后面的那个数与前面的那个数毫无关系。
public class RandomHelper
{
//随机数对象
private Random _random;
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public RandomHelper()
{
//为随机数对象赋值
this._random = new Random();
}
#endregion
#region 生成一个指定范围的随机整数
public int GetRandomInt(int minNum, int maxNum)
{
return this._random.Next(minNum, maxNum);
}
#endregion
#region 生成一个0.0到1.0的随机小数
public double GetRandomDouble()
{
return this._random.NextDouble();
}
#endregion
#region 对一个数组进行随机排序
public void GetRandomArray<T>(T[] arr)
{
//对数组进行随机排序的算法:随机选择两个位置,将两个位置上的值交换
//交换的次数,这里使用数组的长度作为交换次数
int count = arr.Length;
//开始交换
for (int i = 0; i < count; i++)
{
//生成两个随机数位置
int randomNum1 = GetRandomInt(0, arr.Length);
int randomNum2 = GetRandomInt(0, arr.Length);
//定义临时变量
T temp;
//交换两个随机数位置的值
temp = arr[randomNum1];
arr[randomNum1] = arr[randomNum2];
arr[randomNum2] = temp;
}
}
#endregion
#region 随机生成不重复数字字符串
private int rep = 0;
public string GenerateCheckCodeNum(int codeCount)
{
string str = string.Empty;
long num2 = DateTime.Now.Ticks + this.rep;
this.rep++;
Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
for (int i = 0; i < codeCount; i++)
{
int num = random.Next();
str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
}
return str;
}
#endregion
#region 随机生成字符串(数字和字母混和)
public string GenerateCheckCode(int codeCount)
{
string str = string.Empty;
long num2 = DateTime.Now.Ticks + this.rep;
this.rep++;
Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
for (int i = 0; i < codeCount; i++)
{
char ch;
int num = random.Next();
if ((num % 2) == 0)
{
ch = (char)(0x30 + ((ushort)(num % 10)));
}
else
{
ch = (char)(0x41 + ((ushort)(num % 0x1a)));
}
str = str + ch.ToString();
}
return str;
}
#endregion
#region 从字符串里随机得到,规定个数的字符串
private string GetRandomCode(string allChar, int CodeCount)
{
//string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string RandomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < CodeCount; i++)
{
if (temp != -1)
{
rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(allCharArray.Length - 1);
while (temp == t)
{
t = rand.Next(allCharArray.Length - 1);
}
temp = t;
RandomCode += allCharArray[t];
}
return RandomCode;
}
#endregion
}
版权声明
本文为[IT技术猿猴]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baobingji/article/details/124361310
边栏推荐
- .104History
- Detailed deployment of flask project
- Commonly used functions -- spineros:: and spineros::)
- 198. Looting - Dynamic Planning
- Utilisation de la liste - Ajouter, supprimer et modifier la requête
- undefined reference to `Nabo::NearestNeighbourSearch
- The ultimate experience, the audio and video technology behind the tiktok
- Hcip fifth experiment
- C1小笔记【任务训练篇一】
- Client example analysis of easymodbustcp
猜你喜欢
The ultimate experience, the audio and video technology behind the tiktok
958. Complete binary tree test
Go的Gin框架学习
Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Element calculation distance and event object
92. Reverse linked list II byte skipping high frequency question
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
C1小笔记【任务训练篇一】
Land cover / use data product download
随机推荐
1217_ Generating target files using scons
ES6 new method
JS get link? The following parameter name or value, according to the URL? Judge the parameters after
On the method of outputting the complete name of typeID from GCC
列錶的使用-增删改查
386. 字典序排数(中等)-迭代-全排列
QTableWidget使用讲解
Realsense selection comparison d455 d435i d415 t265 3D hardware comparison
Sword finger offer 22 The penultimate node in the linked list - speed pointer
Go的Gin框架学习
2022年流动式起重机司机国家题库模拟考试平台操作
587. 安装栅栏 / 剑指 Offer II 014. 字符串中的变位词
Land cover / use data product download
Element calculation distance and event object
198. 打家劫舍-动态规划
Compilation principle first set follow set select set prediction analysis table to judge whether the symbol string conforms to the grammar definition (with source code!!!)
MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
一些问题一些问题一些问题一些问题
Halo 开源项目学习(二):实体类与数据表
48. 旋转图像