Randomize two List<string> in C# in same order

I have two string lists en and en1

List<string> en = new List<string>(new string[] { "horse", "cat", "dog", "milk", "honey"});
List<string> en1 = new List<string>(new string[] { "horse1", "cat2", "dog3", "milk4", "honey5" });

And I want radomize their content "Shuffle them" and put this radomized content to new two lists. But I also want them randomize same way so lists after randomization will still be

en[0] == en1[0]

random content after randomization

{ "cat", "horse", "honey", "milk", "dog"}
{ "cat2", "horse1", "honey5", "milk4", "dog3"}
Jon Skeet
people
quotationmark

Two obvious ways:

  • Take a normal shuffle method, and change it to modify both lists at the same time
  • Transform the two lists into a single joint list, shuffle that, then split them again

The second sounds cleaner to me. You'd use something like:

var joined = en.Zip(en1, (x, y) => new { x, y }).ToList();
var shuffled = joined.Shuffle(); // Assume this exists
en = shuffled.Select(pair => pair.x).ToList();
en1 = shuffled.Select(pair => pair.y).ToList();

people

See more on this question at Stackoverflow