如何在相同索引下随机混合List内容
How to randomly mix List content under the same indexes
我想知道,什么是在列表中随机混合 List<string> list = new List<string>();
内容而不使用另一个列表进行随机重写的正确方法。
例如,如果我以这种方式通过索引获取值:
list[0];
list[1];
list[2];
list[3];
订单输出为:
line1
line2
line3
line4
在所需的混合之后,如何在相同的索引下获得它,但在随机混合的值上:
list[0];
list[1];
list[2];
list[3];
获取列表中的随机顺序:
line2
line4
line1
line3
试试这个:
// Create an randomizer object
Random r = new Random();
// Create a list of strings
List<string> list = new List<string>() { "line1", "line2", "line3", "line4" };
// Sort list randomly
list = list.OrderBy(x=>r.Next()).ToList();
我想知道,什么是在列表中随机混合 List<string> list = new List<string>();
内容而不使用另一个列表进行随机重写的正确方法。
例如,如果我以这种方式通过索引获取值:
list[0];
list[1];
list[2];
list[3];
订单输出为:
line1
line2
line3
line4
在所需的混合之后,如何在相同的索引下获得它,但在随机混合的值上:
list[0];
list[1];
list[2];
list[3];
获取列表中的随机顺序:
line2
line4
line1
line3
试试这个:
// Create an randomizer object
Random r = new Random();
// Create a list of strings
List<string> list = new List<string>() { "line1", "line2", "line3", "line4" };
// Sort list randomly
list = list.OrderBy(x=>r.Next()).ToList();