首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求八个 不重复的 随机数

2013-07-11 
求8个 不重复的 随机数Listint list new Listint()bool end trueint row -1while (end){bool

求8个 不重复的 随机数

List<int> list = new List<int>();
                bool end = true;
                int row = -1;
                while (end)
                {
                    bool b = true;
                    if (list.Count == 0)
                    {
                        row = new Random().Next(dealerList.Rows.Count);
                        list.Add(row);
                        continue;
                    }
                    row = new Random().Next(dealerList.Rows.Count);
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i] == row)
                        {
                            b = false;
                            break;
                        }


                    }
                    if (b)
                    {
                        list.Add(row);
                    }
                    if (list.Count == 8)
                    {
                        end = false;
                    }
                }


这是我想到的还有跟简明的方法吗?赐教~! 随机数
[解决办法]
这个其实更简洁一些
            List<int> list = new List<int>();
            var random = new Random(Environment.TickCount);
            while(list.Count < 8)
            {
                var x = random.Next(int.MinValue, int.MaxValue);
                if (!list.Contains(x))
                    list.Add(x);
            }

热点排行