当前位置:网站首页>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
边栏推荐
- vite配置proxy代理解决跨域
- Leak detection and vacancy filling (6)
- Cross domain settings of Chrome browser -- including new and old versions
- Realsense selection comparison d455 d435i d415 t265 3D hardware comparison
- 2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
- Summary of common server error codes
- 48. Rotate image
- Operation of 2022 mobile crane driver national question bank simulation examination platform
- The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
- C# 的数据流加密与解密
猜你喜欢
2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
JS get link? The following parameter name or value, according to the URL? Judge the parameters after
2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
48. Rotate image
Cloud native Virtualization: building edge computing instances based on kubevirt
JS forms the items with the same name in the array object into the same array according to the name
2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试
2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
On the method of outputting the complete name of typeID from GCC
Gets the time range of the current week
随机推荐
Dry goods | how to extract thumbnails quickly?
Go's gin framework learning
Fashion classification case based on keras
I/O多路复用及其相关详解
Go对文件操作
2022制冷与空调设备运行操作判断题及答案
48. Rotate image
Remember using Ali Font Icon Library for the first time
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
Comparison between xtask and kotlin coroutine
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!!!)
958. Complete binary tree test
Element calculation distance and event object
Implementation of image recognition code based on VGg convolutional neural network
An example of linear regression based on tensorflow
Classification of cifar100 data set based on convolutional neural network
Notes on common basic usage of eigen Library
Clion installation tutorial
Halo 开源项目学习(二):实体类与数据表
2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition