/// <summary>
/// 生成不重复的随机数
/// </summary>
/// <param name="as_arrNums">数组</param>
/// <param name="as_temp">中间变量,记录数组循环遍历次数</param>
/// <param name="as_minNum">随机数>=最小值</param>
/// <param name="as_maxNum">随机数最大值(不能等于)</param>
/// <param name="as_random">random 对象</param>
/// <returns></returns>
public int getRanNumber(int[] as_arrNums, int as_temp, int as_minNum, int as_maxNum, Random as_random)
{
int ls_row = 0;
while (ls_row < as_arrNums.Length)
{ //循环遍历数组如果有重复的则重新取值
//数组元素默认值为0 所以第1次传入的as_temp的值为0会重新取值
if (as_temp == as_arrNums[ls_row])
{
as_temp = as_random.Next(as_minNum, as_maxNum);
as_temp = getRanNumber(as_arrNums, as_temp, as_minNum, as_maxNum, as_random);
}
ls_row++;
}
return as_temp;
} |
|