fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace RandomTest
  5. {
  6. static class RandomInts
  7. {
  8. private static Random random = new Random();
  9.  
  10. public static HashSet<int> Shuffle(int howMany, int from, int to)
  11. {
  12. CheckRange(howMany, from, to);
  13.  
  14. var result = new HashSet<int>();
  15. while (result.Count < howMany)
  16. {
  17. result.Add(random.Next(from, to + 1));
  18. }
  19. return result;
  20. }
  21.  
  22. private static void CheckRange(int howMany, int from, int to)
  23. {
  24. int count = 0;
  25. for (int i = from; i < to + 1; i++, count++) ;
  26.  
  27. if (howMany > count)
  28. throw new Exception("Inifinite looped range exception.");
  29. }
  30. }
  31.  
  32. class Program
  33. {
  34. static void Main(string[] args)
  35. {
  36. foreach (int i in RandomInts.Shuffle(howMany: 10, from: 1, to: 100))
  37. {
  38. Console.WriteLine(i);
  39. }
  40. }
  41. }
  42. }
Success #stdin #stdout 0.03s 16704KB
stdin
Standard input is empty
stdout
93
34
37
27
52
92
20
22
46
69