本帖最后由 彭家贰小姐 于 2013-6-26 22:34 编辑
- using System;
- using System.Collections.Generic;
- namespace test11
- {
- internal class Program
- {
- //用C#如何在1到1000中随机获取20个数 不重复
- private static void Main(string[] args)
- {
- var count = 0;
- var result = new List<int>();
- var rand = new Random();
- while (true)
- {
- var i = rand.Next(1, 1000);
- if (!result.Contains(i))
- {
- result.Add(i);
- count++;
- }
- if (count >= 20) break;
- }
- foreach (var r in result)
- {
- Console.Write("{0} ", r);
- }
- Console.ReadKey();
- }
- }
- }
复制代码 可实现 不重复
|