Best way to randomize an array with .NET

后端 未结 17 1138
隐瞒了意图╮
隐瞒了意图╮ 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:45

    Here's a simple way using OLINQ:

    // Input array
    List lst = new List();
    for (int i = 0; i < 500; i += 1) lst.Add(i.ToString());
    
    // Output array
    List lstRandom = new List();
    
    // Randomize
    Random rnd = new Random();
    lstRandom.AddRange(from s in lst orderby rnd.Next(100) select s);
    

提交回复
热议问题