c# 如何生成10位随机数

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 05:22:06
c# 如何生成10位随机数

c# 如何生成10位随机数
c# 如何生成10位随机数

c# 如何生成10位随机数
//方法1
public static int[] GetRandom1(int minValue,int maxValue,int count)
{
Random rnd = new Random();
int length = maxValue - minValue + 1;
byte[] keys = new byte[length];
rnd.NextBytes(keys);
int[] items = new int[length];
for (int i = 0; i < length; i++)
{
items[i] = i + minValue;
}
Array.Sort(keys,items);
int[] result = new int[count];
Array.Copy(items,result,count);
return result;
}
//方法2
public static int[] GetRandom2(int minValue,int maxValue,int count)
{
int[] intList = new int[maxValue];
for (int i = 0; i < maxValue; i++)
{
intList[i] = i + minValue;
}
int[] intRet = new int[count];
int n = maxValue;
Random rand = new Random();
for (int i = 0; i < count; i++)
{
int index = rand.Next(0,n);
intRet[i] = intList[index];
intList[index] = intList[--n];
}
return intRet;
}
}
其实方法有很多,采用递归、HASHTABLE、集合等都可以实现