当前位置:网站首页>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
边栏推荐
- 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
- 2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
- 【Appium】通过设计关键字驱动文件来编写脚本
- JS interview question: FN call. call. call. Call (FN2) parsing
- 958. Complete binary tree test
- SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]
- 列表的使用-增删改查
- ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
- Go的Gin框架学习
- Leak detection and vacancy filling (6)
猜你喜欢

Cross domain settings of Chrome browser -- including new and old versions

2022江西储能技术展会,中国电池展,动力电池展,燃料电池展

Kubernetes 服务发现 监控Endpoints

关于gcc输出typeid完整名的方法

.104History

Open source key component multi_ Button use, including test engineering

1217_ Generating target files using scons

编译原理 求first集 follow集 select集预测分析表 判断符号串是否符合文法定义(有源码!!!)

The ultimate experience, the audio and video technology behind the tiktok

2021 Great Wall Cup WP
随机推荐
2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
JS implementation private attribute
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
SystemVerilog (VI) - variable
ES6 face test questions (reference documents)
2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
C#字节数组(byte[])和字符串相互转换
2021 Great Wall Cup WP
239. Maximum value of sliding window (difficult) - one-way queue, large top heap - byte skipping high frequency problem
20222 return to the workplace
Future usage details
The method of changing a value in the array and a value in the object of wechat applet
Some questions some questions some questions some questions
402. Remove K digits - greedy
Where is the configuration file of tidb server?
How to read literature
C1 notes [task training chapter I]
Go语言JSON包使用
Commonly used functions -- spineros:: and spineros::)
Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战