Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Option 1: zip, shuffle, unzip</strong></p> <p>To expand on Marcelo's comment, and assuming you don't mind creating <em>new</em> lists instead of shuffling the <em>existing</em> ones:</p> <pre><code>var zipped = listA.Zip(listB, (a, b) =&gt; new { a, b } ).ToList(); Shuffle(zipped); var newListA = zipped.Select(pair =&gt; pair.a).ToList(); var newListB = zipped.Select(pair =&gt; pair.b).ToList(); </code></pre> <p><strong>Option 2: shuffle the indexes</strong></p> <p>To expand on MAK's answer with code:</p> <pre><code>var indexes = Enumerable.Range(0, listA.Count).ToList(); Shuffle(indexes); var newListA = indexes.Select(index =&gt; listA[index]).ToList(); var newListB = indexes.Select(index =&gt; listB[index]).ToList(); </code></pre> <p>Of course both approaches <em>could</em> mutate the original lists, with a bit more work.</p> <p><strong>Option 3: shuffle both lists with the same random seed</strong></p> <p>I personally like to pass a <code>Random</code> (or whatever) into methods/classes which need them instead of creating new ones. So I'd give my <code>Shuffle</code> a <code>Random</code> parameter. It avoids various problems, and expresses the dependency nicely. You can use this to your advantage, by creating two <code>Random</code> instances with the same seed:</p> <pre><code>int seed = existingRandom.Next(); Shuffle(listA, new Random(seed)); Shuffle(listB, new Random(seed)); </code></pre> <p>Assuming <code>Shuffle</code> does the same thing when given the same sequence of random numbers, this will shuffle both lists in the same way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload