/// <summary>
/// 产生随机数字的数组
/// </summary>
/// <param name="num">个数</param>
/// <param name="minValue">起始值</param>
/// <param name="maxValue">最大值</param>
/// <returns></returns>
public int[] getRandomNum(int num, int minValue, int maxValue)
{
//使用系统时间作为随机种子
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int[] arrNum = new int[num];
int tmp = 0;
bool notRepeat;
for (int i = 0; i <= num - 1; )
{
tmp = ra.Next(minValue, maxValue); //随机取数
notRepeat = true;
for (int j = 0; j < i; j++)
if (tmp == arrNum[j])
{
notRepeat = false;
break;
}
if (notRepeat) arrNum[i++] = tmp;
}
return arrNum;
}
protected void Button1_Click(object sender, EventArgs e)
{
int[] result = getRandomNum(7, 0, 9);
string temp = "";
for (int i = 0; i < result.Length; i++)
{
temp += result[i].ToString() + " ";
}
TextBox1.Text = temp;
}
温馨提示:答案为网友推荐,仅供参考