当前位置:网站首页>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
边栏推荐
- 1217_ Generating target files using scons
- 干货 | 快速抽取缩略图是怎么练成的?
- JS get link? The following parameter name or value, according to the URL? Judge the parameters after
- Kubernetes 服务发现 监控Endpoints
- Element calculation distance and event object
- [binary number] maximum depth of binary tree + maximum depth of n-ary tree
- 402. 移掉 K 位数字-贪心
- Cloud native Virtualization: building edge computing instances based on kubevirt
- QTableWidget使用讲解
- Go的Gin框架学习
猜你喜欢

Anchor location - how to set the distance between the anchor and the top of the page. The anchor is located and offset from the top

Welcome to the markdown editor

Land cover / use data product download

Tell the truth of TS

Applet learning notes (I)

SystemVerilog (VI) - variable

470. 用 Rand7() 实现 Rand10()

Operation of 2022 mobile crane driver national question bank simulation examination platform

Cloud native Virtualization: building edge computing instances based on kubevirt

Go language JSON package usage
随机推荐
209. Minimum length subarray - sliding window
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
Write a regular
Client example analysis of easymodbustcp
ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
cv_ Solution of mismatch between bridge and opencv
C1 notes [task training chapter I]
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
Sword finger offer 22 The penultimate node in the linked list - speed pointer
Halo 开源项目学习(二):实体类与数据表
开源按键组件Multi_Button的使用,含测试工程
Summary of common server error codes
ES6
Leak detection and vacancy filling (6)
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
Amount input box, used for recharge and withdrawal
2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
Applet learning notes (I)
Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
Tdan over half