asp.net 無重復(fù)隨機(jī)數(shù)代碼
來源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 22:44:54
asp.net 無重復(fù)隨機(jī)數(shù)代碼
asp.net 無重復(fù)隨機(jī)數(shù)代碼: 代碼如下:/// <summary> /// 產(chǎn)生無重復(fù)隨機(jī)數(shù) /// </summary> /// <param name=num>個(gè)數(shù)</param> /// <param name=minValue>最小數(shù)</param> /// <param nam
導(dǎo)讀asp.net 無重復(fù)隨機(jī)數(shù)代碼: 代碼如下:/// <summary> /// 產(chǎn)生無重復(fù)隨機(jī)數(shù) /// </summary> /// <param name=num>個(gè)數(shù)</param> /// <param name=minValue>最小數(shù)</param> /// <param nam
代碼如下:
/// <summary>
/// 產(chǎn)生無重復(fù)隨機(jī)數(shù)
/// </summary>
/// <param name="num">個(gè)數(shù)</param>
/// <param name="minValue">最小數(shù)</param>
/// <param name="maxValue">最大數(shù)</param>
/// <returns>數(shù)組</returns>
public int[] getNum(int num, int minValue, int maxValue)
{
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int[] arrNum = new int[num];//注意:數(shù)組中各元素的初始值是0,當(dāng)0在取值范圍內(nèi)時(shí)要另處理
int tmp = 0;
for (int i = 0; i < num; i++)
{
tmp = getRandomNum(tmp, minValue, maxValue, ra);//取出值賦到數(shù)組中
if (Array.IndexOf(arrNum, tmp) < 0)//判斷是否存在,不存在的話元素tmp的索引應(yīng)為-1
{
arrNum[i] = tmp;
}
else
{
i = i - 1;
}
}
return arrNum;
}
public int getRandomNum(int tmp, int minValue, int maxValue, Random ra)
{
tmp = ra.Next(minValue, maxValue);
return tmp;
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
asp.net 無重復(fù)隨機(jī)數(shù)代碼
asp.net 無重復(fù)隨機(jī)數(shù)代碼: 代碼如下:/// <summary> /// 產(chǎn)生無重復(fù)隨機(jī)數(shù) /// </summary> /// <param name=num>個(gè)數(shù)</param> /// <param name=minValue>最小數(shù)</param> /// <param nam