当前位置:网站首页>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
边栏推荐
- 2021长城杯WP
- Tell the truth of TS
- Special effects case collection: mouse planet small tail
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- C1小笔记【任务训练篇一】
- 958. 二叉树的完全性检验
- Submit local warehouse and synchronize code cloud warehouse
- 01 - get to know the advantages of sketch sketch
- ES6 face test questions (reference documents)
- 2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
猜你喜欢

JVM class loading mechanism

958. Complete binary tree test

Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
![[appium] write scripts by designing Keyword Driven files](/img/05/536701f39dcf8474e90e58738f2094.png)
[appium] write scripts by designing Keyword Driven files

JS forms the items with the same name in the array object into the same array according to the name

Summary of floating point double precision, single precision and half precision knowledge

Future usage details

On the method of outputting the complete name of typeID from GCC

470. 用 Rand7() 实现 Rand10()

2021长城杯WP
随机推荐
Land cover / use data product download
cv_ Solution of mismatch between bridge and opencv
This point in JS
2022 tea artist (primary) examination simulated 100 questions and simulated examination
JS parsing and execution process
Listen for click events other than an element
[binary number] maximum depth of binary tree + maximum depth of n-ary tree
122. 买卖股票的最佳时机 II-一次遍历
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
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
On the method of outputting the complete name of typeID from GCC
402. Remove K digits - greedy
SystemVerilog (VI) - variable
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!!!)
2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
JVM class loading mechanism
470. Rand10() is implemented with rand7()
Gaode map search, drag and drop query address
Where is the configuration file of tidb server?