当前位置:网站首页>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
边栏推荐
- Halo open source project learning (II): entity classes and data tables
- On the method of outputting the complete name of typeID from GCC
- JS high frequency interview questions
- 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
- Utilisation de la liste - Ajouter, supprimer et modifier la requête
- cartographer_ There is no problem compiling node, but running the bug that hangs directly
- 2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试
- MySQL 中的字符串函数
- 极致体验,揭晓抖音背后的音视频技术
- 一些问题一些问题一些问题一些问题
猜你喜欢
Go的Gin框架学习
2022 tea artist (primary) examination simulated 100 questions and simulated examination
Open source key component multi_ Button use, including test engineering
cv_ Solution of mismatch between bridge and opencv
Eigen learning summary
2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
Future usage details
2021长城杯WP
SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]
Transfer learning of five categories of pictures based on VGg
随机推荐
C#的随机数生成
Index: teach you index from zero basis to proficient use
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
Romance in C language
ES6 new method
Go file operation
JS parsing and execution process
The method of changing a value in the array and a value in the object of wechat applet
Go的Gin框架学习
On the method of outputting the complete name of typeID from GCC
Flask项目的部署详解
QTableWidget使用讲解
102. Sequence traversal of binary tree
Vite configure proxy proxy to solve cross domain
122. 买卖股票的最佳时机 II-一次遍历
Element calculation distance and event object
Go's gin framework learning
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
In JS, t, = > Analysis of
【Appium】通过设计关键字驱动文件来编写脚本