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.

152 lines
5.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Apewer.Internals
  5. {
  6. internal class RandomHelper
  7. {
  8. /// <summary>生成新的 GUID。</summary>
  9. public static string NewGuid(bool argHyphenation = false, bool argLowerCase = true)
  10. {
  11. var vguid = System.Guid.NewGuid().ToString();
  12. if (argLowerCase) vguid = vguid.ToLower();
  13. else vguid = vguid.ToUpper();
  14. if (!argHyphenation) vguid = vguid.Replace("-", "");
  15. return vguid;
  16. }
  17. /// <summary>用于生成随机数的时钟。</summary>
  18. private static float RandomTimer()
  19. {
  20. var now = DateTime.Now;
  21. return (float)((double)(checked((60 * now.Hour + now.Minute) * 60 + now.Second)) + (double)now.Millisecond / 1000.0);
  22. }
  23. /// <summary>用于生成随机数的时钟位置。</summary>
  24. private static int RandomSeed = 327680;
  25. /// <summary>初始化随机数生成。</summary>
  26. private static void Randomize()
  27. {
  28. RandomInit();
  29. }
  30. /// <summary>初始化随机数生成。</summary>
  31. public static void RandomInit()
  32. {
  33. float vtimer = RandomTimer();
  34. int vnum1 = RandomSeed;
  35. int vnum2 = BitConverter.ToInt32(BitConverter.GetBytes(vtimer), 0);
  36. vnum2 = ((vnum2 & 65535) ^ vnum2 >> 16) << 8;
  37. vnum1 = ((vnum1 & -16776961) | vnum2);
  38. RandomSeed = vnum1;
  39. }
  40. /// <summary>初始化随机数生成。</summary>
  41. public static void RandomInit(double argNumber)
  42. {
  43. int vnum1 = RandomSeed;
  44. int vnum2 = BitConverter.ToInt32(BitConverter.GetBytes(argNumber), (BitConverter.IsLittleEndian ? 4 : 8));
  45. vnum2 = ((vnum2 & 65535) ^ vnum2 >> 16) << 8;
  46. vnum1 = ((vnum1 & -16776961) | vnum2);
  47. RandomSeed = vnum1;
  48. }
  49. /// <summary>生成一个不大于 1 的随机单精度浮点数,最小为 0。</summary>
  50. public static float RandomFloat()
  51. {
  52. return RandomFloat(1f);
  53. }
  54. /// <summary>生成一个随机单精度浮点数。</summary>
  55. public static float RandomFloat(float argNumber)
  56. {
  57. int vnum1 = RandomSeed;
  58. if ((double)argNumber != 0.0)
  59. {
  60. if ((double)argNumber < 0.0)
  61. {
  62. vnum1 = BitConverter.ToInt32(BitConverter.GetBytes(argNumber), 0);
  63. long vnum2 = (long)vnum1;
  64. vnum2 &= unchecked((long)((ulong)-1));
  65. vnum1 = checked((int)(vnum2 + (vnum2 >> 24) & 16777215L));
  66. }
  67. vnum1 = checked((int)(unchecked((long)vnum1) * 1140671485L + 12820163L & 16777215L));
  68. }
  69. RandomSeed = vnum1;
  70. return (float)vnum1 / 16777216f;
  71. }
  72. /// <summary>生成随机整数,最小为 0。</summary>
  73. /// <param name="argMax">最大数。</param>
  74. /// <returns></returns>
  75. public static int RandomInteger(int argMax)
  76. {
  77. return RandomInteger(0, argMax);
  78. }
  79. /// <summary>生成随机整数。</summary>
  80. /// <param name="argMin">最小数。</param>
  81. /// <param name="argMax">最大数。</param>
  82. /// <returns></returns>
  83. public static int RandomInteger(int argMin, int argMax)
  84. {
  85. RandomInit();
  86. float vfloat = (argMax - argMin + 1) * RandomFloat();
  87. int vint = argMin + (int)vfloat;
  88. return vint;
  89. }
  90. /// <summary>由指定字符成的随机字符串。</summary>
  91. /// <param name="argPool">字符池,字符池中每个字符在随机字符串中出现的概率约等。</param>
  92. /// <param name="argLength">随机字符串的长度。</param>
  93. public static string RandomCustom(string argPool, int argLength)
  94. {
  95. switch (TextHelper.Len(argPool))
  96. {
  97. case 0:
  98. return TextGenerator.Space(argLength);
  99. case 1:
  100. return TextGenerator.CopyChar(argPool, argLength);
  101. default:
  102. var vresult = new StringBuilder();
  103. int vl = TextHelper.Len(argPool);
  104. string vc;
  105. float vn;
  106. float vs;
  107. Randomize();
  108. while (vresult.Length < argLength)
  109. {
  110. vn = (vl + 1) * RandomFloat();
  111. vs = Convert.ToInt32(Math.Floor(vn));
  112. if (vs > 0)
  113. {
  114. if (vs > vl) vs = vl;
  115. vc = TextHelper.Mid(argPool, Convert.ToInt32(vs), 1);
  116. vresult.Append(vc);
  117. }
  118. }
  119. return vresult.ToString();
  120. }
  121. }
  122. /// <summary>由易识别字符组成的随机字符串。</summary>
  123. /// <param name="argLength">随机字符串的长度。</param>
  124. public static string RandomLucid(int argLength)
  125. {
  126. return TextHelper.LCase(RandomCustom(Constant.LucidCollection, argLength));
  127. }
  128. /// <summary>由数字和字母组成的随机字符串。</summary>
  129. /// <param name="argLength">随机字符串的长度。</param>
  130. public static string RandomSimple(int argLength)
  131. {
  132. return TextHelper.LCase(RandomCustom(Constant.NumberCollection + Constant.LowerCollection, argLength));
  133. }
  134. }
  135. }