fork download
  1. using static System.Console;
  2. using System.Collections.Generic;
  3.  
  4. public static class Sorteio {
  5. public static void Main() {
  6. var lista = new List<string>() { "Alaor", "Joseval", "Salustiano", "Gomide", "Castro" };
  7. lista.Shuffle();
  8. foreach (var valor in lista) {
  9. WriteLine(valor);
  10. }
  11. WriteLine("//////////");
  12. string[] array = { "Alaor", "Joseval", "Salustiano", "Gomide", "Castro" };
  13. array.Shuffle(2);
  14. foreach (var valor in array) {
  15. WriteLine(valor);
  16. }
  17. WriteLine("//////////");
  18. int[] array2 = { 1, 2, 3, 4, 5 };
  19. array2.Shuffle(1,4);
  20. foreach (var valor in array2) {
  21. WriteLine(valor);
  22. }
  23. }
  24. }
  25.  
  26. namespace System.Collections.Generic {
  27. public static class IListExt {
  28. static Random r = new Random(DateTime.Now.Millisecond);
  29.  
  30. public static void Shuffle<T>(this IList<T> list, int lowerItem, int upperItem) {
  31. upperItem = upperItem > list.Count ? list.Count : upperItem;
  32. lowerItem = lowerItem < 0 ? 0 : lowerItem;
  33. for (int i = lowerItem; i < upperItem; i++) {
  34. int j = r.Next(i, upperItem);
  35. T tmp = list[j];
  36. list[j] = list[i];
  37. list[i] = tmp;
  38. }
  39. }
  40.  
  41. public static void Shuffle<T>(this IList<T> list, int upperItem) {
  42. list.Shuffle(0, upperItem);
  43. }
  44.  
  45. public static void Shuffle<T>(this IList<T> list) {
  46. list.Shuffle(0, list.Count);
  47. }
  48. }
  49. }
  50.  
  51. //https://pt.stackoverflow.com/q/17783/101
Success #stdin #stdout 0.07s 26124KB
stdin
Standard input is empty
stdout
Gomide
Alaor
Joseval
Castro
Salustiano
//////////
Joseval
Alaor
Salustiano
Gomide
Castro
//////////
1
3
2
4
5