注意,随机数范围,取下限值,但不取上限值。
//随机数放入数组,但不能重复
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); });