Best way to randomize an array with .NET

后端 未结 17 892
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 01:55

What is the best way to randomize an array of strings with .NET? My array contains about 500 strings and I\'d like to create a new Array with the same strings b

17条回答
  •  無奈伤痛
    2020-11-22 02:24

            int[] numbers = {0,1,2,3,4,5,6,7,8,9};
            List numList = new List();
            numList.AddRange(numbers);
    
            Console.WriteLine("Original Order");
            for (int i = 0; i < numList.Count; i++)
            {
                Console.Write(String.Format("{0} ",numList[i]));
            }
    
            Random random = new Random();
            Console.WriteLine("\n\nRandom Order");
            for (int i = 0; i < numList.Capacity; i++)
            {
                int randomIndex = random.Next(numList.Count);
                Console.Write(String.Format("{0} ", numList[randomIndex]));
                numList.RemoveAt(randomIndex);
            }
            Console.ReadLine();
    

提交回复
热议问题