Random.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. namespace TBL.CSharp.Utilities.Random
  3. {
  4. /// <summary>
  5. /// 随机数扩展工具类
  6. /// </summary>
  7. public static class Random
  8. {
  9. /// <summary>
  10. /// 生成非负随机数
  11. /// </summary>
  12. public static int Next(this IRandom random) => Math.Abs(Math.Max(random.NextRaw(), int.MinValue + 1));
  13. /// <summary>
  14. /// 生成随机数,并将结果取余到范围内
  15. /// </summary>
  16. /// <param name="random">随机数发生器</param>
  17. /// <param name="minValue">随机最小值(结果包含)</param>
  18. /// <param name="maxValue">随机最大值(结果不包含)</param>
  19. /// <returns>生成结果</returns>
  20. /// <exception cref="ArgumentOutOfRangeException">范围上下界不正确</exception>
  21. public static int Next(this IRandom random, int minValue, int maxValue)
  22. {
  23. if (minValue >= maxValue)
  24. throw new ArgumentOutOfRangeException(nameof(minValue),
  25. $"{nameof(minValue)}({minValue}) >= {nameof(maxValue)}({maxValue})");
  26. return minValue + random.Next() % (maxValue - minValue);
  27. }
  28. /// <summary>
  29. /// 生成 <c>0</c> ~ <paramref name="maxValue"/> 内随机数
  30. /// </summary>
  31. /// <param name="random">随机数发生器</param>
  32. /// <param name="maxValue">随机最大值(结果不包含)</param>
  33. /// <returns>生成结果</returns>
  34. /// <exception cref="ArgumentOutOfRangeException">范围上下界不正确</exception>
  35. public static int Next(this IRandom random, int maxValue) => random.Next(0, maxValue);
  36. /// <summary>
  37. /// 进行概率取模检定
  38. /// </summary>
  39. /// <param name="random">随机数发生器</param>
  40. /// <param name="minValue">随机最小值(结果包含)</param>
  41. /// <param name="maxValue">随机最大值(结果不包含)</param>
  42. /// <param name="line">检定分界线</param>
  43. /// <returns>随机结果对 <paramref name="line"/> 的取模值</returns>
  44. public static int CheckModule(this IRandom random, int minValue, int maxValue, int line) =>
  45. random.Next(minValue, maxValue) % line;
  46. /// <summary>
  47. /// 以百分比进行概率取模检定
  48. /// </summary>
  49. /// <param name="random">随机数发生器</param>
  50. /// <param name="line">检定分界线</param>
  51. /// <returns>在范围 <c>0</c> ~ <c>100</c> 中生成的随机结果对 <paramref name="line"/> 的取模值</returns>
  52. public static int CheckModulePercent(this IRandom random, int line) =>
  53. random.CheckModule(0, 100, line);
  54. /// <summary>
  55. /// 进行概率检定
  56. /// </summary>
  57. /// <param name="random">随机数发生器</param>
  58. /// <param name="minValue">随机最小值(结果包含)</param>
  59. /// <param name="maxValue">随机最大值(结果不包含)</param>
  60. /// <param name="line">检定分界线</param>
  61. /// <returns>随机结果是否小于 <paramref name="line"/></returns>
  62. public static bool Check(this IRandom random, int minValue, int maxValue, int line) =>
  63. random.Next(minValue, maxValue) < line;
  64. /// <summary>
  65. /// 以百分比进行概率检定
  66. /// </summary>
  67. /// <param name="random">随机数发生器</param>
  68. /// <param name="line">检定分界百分比</param>
  69. /// <returns>在范围 <c>0</c> ~ <c>100</c> 中生成的随机结果是否小于 <paramref name="line"/></returns>
  70. public static bool CheckPercent(this IRandom random, int line) => random.Check(0, 100, line);
  71. }
  72. }