fork download
  1. using static System.Console;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public static class Sorteio {
  6. public static void Main() {
  7. var lista = new List<string>() { "Alaor", "Joseval", "Salustiano", "Gomide", "Castro" };
  8. foreach (string value in lista.Shuffle()) {
  9. WriteLine(value);
  10. }
  11. WriteLine("////////");
  12. foreach (string value in lista.Shuffle().Take(3)) {
  13. WriteLine(value);
  14. }
  15. }
  16. }
  17.  
  18. namespace System.Collections.Generic {
  19. public static class IEnumerableExt {
  20. static Random r = new Random(DateTime.Now.Millisecond);
  21.  
  22. public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list) {
  23. T[] array = list.ToArray();
  24. for (int i = array.Length - 1; i > 0; i--) {
  25. int j = r.Next(i + 1);
  26. T tmp = array[j];
  27. array[j] = array[i];
  28. array[i] = tmp;
  29. }
  30. foreach(var item in array) {
  31. yield return item;
  32. }
  33. }
  34. }
  35. }
  36.  
  37. //https://pt.stackoverflow.com/q/17783/101
Success #stdin #stdout 0.06s 18816KB
stdin
Standard input is empty
stdout
Alaor
Gomide
Salustiano
Castro
Joseval
////////
Gomide
Joseval
Castro