You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.6 KiB

  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer
  6. {
  7. /// <summary>数字实用工具。</summary>
  8. public class NumberUtility
  9. {
  10. #region 随机数。
  11. /// <summary>用于生成随机数的时钟位置。</summary>
  12. private static int RandomSeed = 327680;
  13. private static float RandomTimer()
  14. {
  15. var now = DateTime.Now;
  16. return (float)((double)(checked((60 * now.Hour + now.Minute) * 60 + now.Second)) + (double)now.Millisecond / 1000.0);
  17. }
  18. private static void RandomInit()
  19. {
  20. float timer = RandomTimer();
  21. int num1 = RandomSeed;
  22. int num2 = BitConverter.ToInt32(BitConverter.GetBytes(timer), 0);
  23. num2 = ((num2 & 65535) ^ num2 >> 16) << 8;
  24. num1 = ((num1 & -16776961) | num2);
  25. RandomSeed = num1;
  26. }
  27. /// <summary>生成不大于 1 的随机数,最小为 0。</summary>
  28. public static float RandomFloat(float max = 1F)
  29. {
  30. RandomInit();
  31. int num1 = RandomSeed;
  32. if ((double)max != 0.0)
  33. {
  34. if ((double)max < 0.0)
  35. {
  36. num1 = BitConverter.ToInt32(BitConverter.GetBytes(max), 0);
  37. long vnum2 = (long)num1;
  38. vnum2 &= unchecked((long)((ulong)-1));
  39. num1 = checked((int)(vnum2 + (vnum2 >> 24) & 16777215L));
  40. }
  41. num1 = checked((int)(unchecked((long)num1) * 1140671485L + 12820163L & 16777215L));
  42. }
  43. RandomSeed = num1;
  44. return (float)num1 / 16777216f;
  45. }
  46. /// <summary>生成随机整数,包含最小值和最大值。</summary>
  47. /// <param name="min">最小值。</param>
  48. /// <param name="max">最大值。</param>
  49. /// <returns></returns>
  50. public static int RandomInteger(int max, int min = 0)
  51. {
  52. if (max < min)
  53. {
  54. var temp = max;
  55. max = min;
  56. min = temp;
  57. }
  58. float rate = (max - min + 1) * RandomFloat();
  59. int result = min + (int)rate;
  60. return result;
  61. }
  62. #endregion
  63. #region Restrict 约束值范围。
  64. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  65. public static T RestrictValue<T>(T origin, T min, T max) where T : IComparable
  66. {
  67. try
  68. {
  69. if (min.CompareTo(max) > 0) return origin;
  70. if (origin.CompareTo(min) < 0) return min;
  71. if (origin.CompareTo(max) > 0) return max;
  72. return origin;
  73. }
  74. catch { }
  75. return origin;
  76. }
  77. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  78. public static Byte RestrictValue(Byte target, Byte min, Byte max)
  79. {
  80. return RestrictValue<Byte>(target, min, max);
  81. }
  82. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  83. public static Int16 RestrictValue(Int16 target, Int16 min, Int16 max)
  84. {
  85. return RestrictValue<Int16>(target, min, max);
  86. }
  87. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  88. public static Int32 RestrictValue(Int32 target, Int32 min, Int32 max)
  89. {
  90. return RestrictValue<Int32>(target, min, max);
  91. }
  92. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  93. public static Int64 RestrictValue(Int64 target, Int64 min, Int64 max)
  94. {
  95. return RestrictValue<Int64>(target, min, max);
  96. }
  97. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  98. public static Single RestrictValue(Single target, Single min, Single max)
  99. {
  100. return RestrictValue<Single>(target, min, max);
  101. }
  102. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  103. public static Double RestrictValue(Double target, Double min, Double max)
  104. {
  105. return RestrictValue<Double>(target, min, max);
  106. }
  107. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  108. public static Decimal RestrictValue(Decimal target, Decimal min, Decimal max)
  109. {
  110. return RestrictValue<Decimal>(target, min, max);
  111. }
  112. #endregion
  113. }
  114. }