当前位置:网站首页>Random number generation of C #
Random number generation of C #
2022-04-23 17:56:00 【It technology ape】
Random numbers are the results of special randomized trials . Random numbers are used in different techniques of statistics , For example, when taking a representative sample from a statistical population , Or in the process of assigning experimental animals to different experimental groups , Or in Monte Carlo simulation, etc . There are many different ways to generate random numbers . These methods are called random number generators . The most important characteristic of random number is : The latter number it produces has nothing to do with the previous number .
public class RandomHelper
{
// Random number object
private Random _random;
#region Constructors
/// <summary>
/// Constructors
/// </summary>
public RandomHelper()
{
// Assign values to random number objects
this._random = new Random();
}
#endregion
#region Generates a random integer in a specified range
public int GetRandomInt(int minNum, int maxNum)
{
return this._random.Next(minNum, maxNum);
}
#endregion
#region Generate a 0.0 To 1.0 A random fraction of
public double GetRandomDouble()
{
return this._random.NextDouble();
}
#endregion
#region Sort an array randomly
public void GetRandomArray<T>(T[] arr)
{
// An algorithm for random sorting of arrays : Randomly choose two positions , Exchange the values in the two positions
// The number of exchanges , Here, the length of the array is used as the number of exchanges
int count = arr.Length;
// Start swapping
for (int i = 0; i < count; i++)
{
// Generate two random number positions
int randomNum1 = GetRandomInt(0, arr.Length);
int randomNum2 = GetRandomInt(0, arr.Length);
// Define temporary variables
T temp;
// Swap the values of two random number positions
temp = arr[randomNum1];
arr[randomNum1] = arr[randomNum2];
arr[randomNum2] = temp;
}
}
#endregion
#region Randomly generate non repeating numeric strings
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 Randomly generate strings ( Mix of numbers and letters )
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 Randomly get... From the string , A specified number of strings
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 technology ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231753552350.html
边栏推荐
猜你喜欢
C#的随机数生成
Applet learning notes (I)
Gets the time range of the current week
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
958. Complete binary tree test
Open source key component multi_ Button use, including test engineering
1217_ Generating target files using scons
Eigen learning summary
cv_ Solution of mismatch between bridge and opencv
随机推荐
Realsense selection comparison d455 d435i d415 t265 3D hardware comparison
2021 Great Wall Cup WP
vite配置proxy代理解决跨域
31. Next arrangement
开源按键组件Multi_Button的使用,含测试工程
【Appium】通过设计关键字驱动文件来编写脚本
Land cover / use data product download
Halo open source project learning (II): entity classes and data tables
Uniapp custom search box adaptation applet alignment capsule
[appium] write scripts by designing Keyword Driven files
土地覆盖/利用数据产品下载
[binary number] maximum depth of binary tree + maximum depth of n-ary tree
Romance in C language
I/O多路复用及其相关详解
Detailed deployment of flask project
.105Location
An example of linear regression based on tensorflow
YOLOv4剪枝【附代码】
Tensorflow tensor introduction
列表的使用-增删改查