fork(14) download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Test
  5. {
  6. static Random random = new Random();
  7.  
  8. public static List<int> GenerateRandom(int count)
  9. {
  10. // generate count random values.
  11. HashSet<int> candidates = new HashSet<int>();
  12. while (candidates.Count < count)
  13. {
  14. // May strike a duplicate.
  15. candidates.Add(random.Next());
  16. }
  17.  
  18. // load them in to a list.
  19. List<int> result = new List<int>();
  20. result.AddRange(candidates);
  21.  
  22. // shuffle the results:
  23. int i = result.Count;
  24. while (i > 1)
  25. {
  26. i--;
  27. int k = random.Next(i + 1);
  28. int value = result[k];
  29. result[k] = result[i];
  30. result[i] = value;
  31. }
  32. return result;
  33. }
  34. public static void Main()
  35. {
  36. List<int> vals = GenerateRandom(10);
  37. Console.WriteLine("Result: " + vals.Count);
  38. vals.ForEach(Console.WriteLine);
  39. }
  40. }
Success #stdin #stdout 0.06s 34080KB
stdin
Standard input is empty
stdout
Result: 10
2070122011
1419642082
2106305672
102665921
1415739618
1013160141
1021833481
1708489576
820147084
1190646335