protected void Page_Load(object sender, EventArgs e)
...{
Set_Random();
}
/**//// <summary>
/// 不重复随机数
/// </summary>
/// <param name="minValue">最小值</param>
/// <param name="maxValue">最大值</param>
/// <param name="count">多少个随机数</param>
/// <returns></returns>
public int[] GetRandomUnrepeatArray(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;
}
public void Set_Random()
...{
//调用产生随机数的方法产生的数据数是1111111到9999999之间的随机数输出10个
int[] items = this.GetRandomUnrepeatArray(0, 10, 10);
for (int i = 0; i < items.Length; i++)
...{
Response.Write("产生的随机数是:" + items[i] + "<br>");
}
}
本文介绍了一种在指定范围内生成不重复随机数数组的方法,并提供了一个具体的实现示例。通过使用.NET Framework中的Random类和数组操作,该方法能够有效地生成指定数量的不重复随机整数。

1041

被折叠的 条评论
为什么被折叠?



