您的位置:首页 > 编程语言 > C#

C#取随机数注意事项

2013-12-08 16:51 309 查看
注意,随机数范围,取下限值,但不取上限值。

//随机数放入数组,但不能重复

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); });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 随机数