fork(5) 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. // top will overflow to Int32.MinValue at the end of the loop
  13. for (Int32 top = Int32.MaxValue - count + 1; top > 0; top++)
  14. {
  15. // May strike a duplicate.
  16. if (!candidates.Add(random.Next(top))) {
  17. candidates.Add(top);
  18. }
  19. }
  20.  
  21. // load them in to a list.
  22. List<int> result = new List<int>();
  23. result.AddRange(candidates);
  24.  
  25. // shuffle the results:
  26. int i = result.Count;
  27. while (i > 1)
  28. {
  29. i--;
  30. int k = random.Next(i + 1);
  31. int value = result[k];
  32. result[k] = result[i];
  33. result[i] = value;
  34. }
  35. return result;
  36. }
  37. public static void Main()
  38. {
  39. List<int> vals = GenerateRandom(10);
  40. Console.WriteLine("Result: " + vals.Count);
  41. vals.ForEach(Console.WriteLine);
  42. }
  43. }
Success #stdin #stdout 0.06s 34080KB
stdin
Standard input is empty
stdout
Result: 10
1811652443
1151444051
826867486
503609639
775991376
287992689
1157058324
1746341663
1407136658
1747036625