注意,隨機數範圍,取下限值,但不取上限值。
//隨機數放入數組,但不能重複
Random random = new Random();//隨機對象
List<int> iList = new List<int>();//列表對象
while (true)
{
int i = random.Next(1, 10);//注意隨機數範圍,取下限值,但不取上限值
bool b = iList.Exists(delegate(int n) { if (n == i)return true; else return false; });
if (!b)
{
iList.Add(i);
}
if (iList.Count > 8) break;
}
iList.Sort();
iList.ForEach(delegate(int i) { Console.WriteLine(i); });