olegstankoptev

Untitled

Apr 27th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. using System;
  2.  
  3. public class RandomExtension : Random
  4. {
  5. int Seed { get; set; }
  6. public RandomExtension(int seed)
  7. {
  8. Seed = seed;
  9. }
  10. public double NextDouble(double minValue, double maxValue)
  11. {
  12. if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
  13. Random gnr = new Random(Seed);
  14. return gnr.NextDouble() * (maxValue - minValue) + minValue;
  15. }
  16.  
  17. private static int NextValue(int number, bool isEven, int minValue, int maxValue)
  18. {
  19. Random gnr = new Random(number);
  20. int rndNum = gnr.Next(minValue, maxValue);
  21. if (rndNum % 2 == 0)
  22. {
  23. if (isEven) return rndNum;
  24. if (rndNum + 1 < maxValue) return rndNum + 1;
  25. if (rndNum - 1 > 0) return rndNum - 1;
  26. throw new ArgumentException("no numbers in this range");
  27. }
  28. if (!isEven) return rndNum;
  29. if (rndNum + 1 < maxValue) return rndNum + 1;
  30. if (rndNum - 1 > 0) return rndNum - 1;
  31. throw new ArgumentException("no numbers in this range");
  32. }
  33.  
  34. public int NextEven(int minValue, int maxValue)
  35. {
  36. if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
  37. return NextValue(Seed, true, minValue, maxValue);
  38. }
  39.  
  40. public int NextOdd(int minValue, int maxValue)
  41. {
  42. if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
  43. return NextValue(Seed, false, minValue, maxValue);
  44. }
  45.  
  46. public char NextChar(char minValue, char maxValue)
  47. {
  48. if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
  49. Random gnr = new Random(Seed);
  50. return (char)gnr.Next(minValue, maxValue);
  51. }
  52.  
  53. public bool NextBool(ushort probability)
  54. {
  55. if (probability > 100) throw new ArgumentException("minValue cannot be greater than maxValue");
  56. Random gnr = new Random(Seed);
  57. if (gnr.NextDouble() * 100 < probability)
  58. {
  59. return true;
  60. }
  61. return false;
  62. }
  63.  
  64. public DateTime NextDate(DateTime minValue, DateTime maxValue)
  65. {
  66. if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
  67. Random gnr = new Random(Seed);
  68. TimeSpan timeSpan = TimeSpan.FromDays(gnr.Next((maxValue - minValue).Days));
  69. return minValue + timeSpan;
  70. }
  71. }
Add Comment
Please, Sign In to add comment