TickBasedRandom.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace MISTBowl_Brackets
  5. {
  6. public static class TickBasedRandom
  7. {
  8. static long ticks;
  9. static Random rng = new Random(0);
  10. public static int GetInteger(int min, int max)
  11. {
  12. try
  13. {
  14. long currentTicks = DateTime.UtcNow.Ticks;
  15. if (currentTicks != ticks)
  16. {
  17. rng = new Random((int)(ticks = currentTicks));
  18. }
  19. return rng.Next(min, max + 1);
  20. }
  21. catch (Exception e)
  22. {
  23. Traceback.Write("At int GetInteger(int, int");
  24. throw e;
  25. }
  26. }
  27. public static T[] SortRandom<T>(this IEnumerable<T> input)
  28. {
  29. try
  30. {
  31. var inputList = input.ToList();
  32. var output = new List<T>();
  33. while (inputList.ToArray().Length > 0)
  34. {
  35. var index = GetInteger(0, inputList.ToArray().Length - 1);
  36. output.Add(inputList.ElementAt(index));
  37. inputList.RemoveAt(index);
  38. }
  39. return output.ToArray();
  40. }
  41. catch (Exception e)
  42. {
  43. Traceback.Write("At T[] SortRandom<T>(IEnumerable<T>) (class TickBasedRandom):");
  44. throw e;
  45. }
  46. }
  47. }
  48. }