码迷,mamicode.com
首页 > Windows程序 > 详细

C# List中随机获取N个字符

时间:2015-04-02 14:45:01      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

 1  static void Main(string[] args)
 2         {
 3             List<string> strList = new List<string>();
 4             for (int i = 1; i <= 50; i++)
 5             {
 6                 strList.Add("string "+i);
 7             }
 8             int number = 10;
 9             if (strList.Count < number)
10             {
11                 number = strList.Count;
12             }
13 
14             int count=0;
15             List<string> resultList = new List<string>();
16             for (int i = 1; i <= number; i++)
17             {
18                 int rang = strList.Count - i;
19                 int index = new Random().Next(0, rang);
20                 //Console.WriteLine("ramg:" + rang + "  strList[" + index + "]:" + strList[index]);
21                 resultList.Add(strList[index]);
22                 strList[index] = strList[rang];
23                 count++;
24             }
25             Console.WriteLine("---------COUNT: " + count);
26             foreach (var result in resultList)
27             {
28                 Console.WriteLine(result);
29             }
30 
31             Console.ReadLine();
32 
33         }

 

C# List中随机获取N个字符

标签:

原文地址:http://www.cnblogs.com/jetlian/p/list-random-string.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!