fork download
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. public struct Trio
  9. {
  10. public int Value1 { get; set; }
  11. public int Value2 { get; set; }
  12. public int Value3 { get; set; }
  13. }
  14.  
  15. public static void Main()
  16. {
  17. List<int> A = new List<int> { 1, 2, 3, 6 };
  18. List<int> B = new List<int> { 4, 5, 6, 8 };
  19. List<int> C = new List<int> { 7, 8, 9, 33 };
  20.  
  21. var aShuffle = new List<int>(A.Count);
  22. aShuffle.AddRange(A.Shuffle());
  23. var bShuffle = new List<int>(B.Count);
  24. bShuffle.AddRange(B.Shuffle());
  25. var cShuffle = new List<int>(C.Count);
  26. cShuffle.AddRange(C.Shuffle());
  27.  
  28. List<Trio> trios = new List<Trio>(aShuffle.Count);
  29. for (int i = 0; i < aShuffle.Count; i++)
  30. {
  31. trios.Add(new Trio { Value1 = aShuffle[i], Value2 = bShuffle[i], Value3 = cShuffle[i] });
  32. }
  33. foreach(Trio trio in trios)
  34. Console.WriteLine("Value1: {0} Value2: {1} Value3: {2}", trio.Value1,trio.Value2,trio.Value3);
  35. }
  36. }
  37.  
  38. public static class Extensions
  39. {
  40. public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
  41. {
  42. return source.Shuffle(new Random());
  43. }
  44.  
  45. public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
  46. {
  47. if (source == null) throw new ArgumentNullException("source");
  48. if (rng == null) throw new ArgumentNullException("rng");
  49.  
  50. return source.ShuffleIterator(rng);
  51. }
  52.  
  53. private static IEnumerable<T> ShuffleIterator<T>(
  54. this IEnumerable<T> source, Random rng)
  55. {
  56. List<T> buffer = source.ToList();
  57. for (int i = 0; i < buffer.Count; i++)
  58. {
  59. int j = rng.Next(i, buffer.Count);
  60. yield return buffer[j];
  61.  
  62. buffer[j] = buffer[i];
  63. }
  64. }
  65. }
Success #stdin #stdout 0.04s 33928KB
stdin
Standard input is empty
stdout
Value1: 3 Value2: 5 Value3: 8
Value1: 1 Value2: 6 Value3: 9
Value1: 6 Value2: 4 Value3: 7
Value1: 2 Value2: 8 Value3: 33