你的位置:首页 > ASP.net教程

[ASP.net教程]C# List中随机获取N个字符


 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     }