fork(4) 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. List<int> result = new List<int>(count);
  11.  
  12. // generate count random values.
  13. HashSet<int> candidates = new HashSet<int>();
  14. // top will overflow to Int32.MinValue at the end of the loop
  15. for (Int32 top = Int32.MaxValue - count + 1; top > 0; top++)
  16. {
  17. // May strike a duplicate.
  18. int value = random.Next(top);
  19. if (candidates.Add(value))
  20. {
  21. result.Add(value);
  22. }
  23. else
  24. {
  25. result.Add(top);
  26. candidates.Add(top);
  27. }
  28. }
  29.  
  30. return result;
  31. }
  32. public static void Main()
  33. {
  34. List<int> vals = GenerateRandom(10);
  35. Console.WriteLine("Result: " + vals.Count);
  36. vals.ForEach(Console.WriteLine);
  37. }
  38. }
Success #stdin #stdout 0.04s 34016KB
stdin
Standard input is empty
stdout
Result: 10
1266576738
1988796064
1296699947
644552315
1095650138
534957753
553590513
474913018
1925059559
1750148947