using System; using System.Collections.Generic; using System.Text; namespace Apewer.Internals { internal class RandomHelper { /// 生成新的 GUID。 public static string NewGuid(bool argHyphenation = false, bool argLowerCase = true) { var vguid = System.Guid.NewGuid().ToString(); if (argLowerCase) vguid = vguid.ToLower(); else vguid = vguid.ToUpper(); if (!argHyphenation) vguid = vguid.Replace("-", ""); return vguid; } /// 用于生成随机数的时钟。 private static float RandomTimer() { var now = DateTime.Now; return (float)((double)(checked((60 * now.Hour + now.Minute) * 60 + now.Second)) + (double)now.Millisecond / 1000.0); } /// 用于生成随机数的时钟位置。 private static int RandomSeed = 327680; /// 初始化随机数生成。 private static void Randomize() { RandomInit(); } /// 初始化随机数生成。 public static void RandomInit() { float vtimer = RandomTimer(); int vnum1 = RandomSeed; int vnum2 = BitConverter.ToInt32(BitConverter.GetBytes(vtimer), 0); vnum2 = ((vnum2 & 65535) ^ vnum2 >> 16) << 8; vnum1 = ((vnum1 & -16776961) | vnum2); RandomSeed = vnum1; } /// 初始化随机数生成。 public static void RandomInit(double argNumber) { int vnum1 = RandomSeed; int vnum2 = BitConverter.ToInt32(BitConverter.GetBytes(argNumber), (BitConverter.IsLittleEndian ? 4 : 8)); vnum2 = ((vnum2 & 65535) ^ vnum2 >> 16) << 8; vnum1 = ((vnum1 & -16776961) | vnum2); RandomSeed = vnum1; } /// 生成一个不大于 1 的随机单精度浮点数,最小为 0。 public static float RandomFloat() { return RandomFloat(1f); } /// 生成一个随机单精度浮点数。 public static float RandomFloat(float argNumber) { int vnum1 = RandomSeed; if ((double)argNumber != 0.0) { if ((double)argNumber < 0.0) { vnum1 = BitConverter.ToInt32(BitConverter.GetBytes(argNumber), 0); long vnum2 = (long)vnum1; vnum2 &= unchecked((long)((ulong)-1)); vnum1 = checked((int)(vnum2 + (vnum2 >> 24) & 16777215L)); } vnum1 = checked((int)(unchecked((long)vnum1) * 1140671485L + 12820163L & 16777215L)); } RandomSeed = vnum1; return (float)vnum1 / 16777216f; } /// 生成随机整数,最小为 0。 /// 最大数。 /// public static int RandomInteger(int argMax) { return RandomInteger(0, argMax); } /// 生成随机整数。 /// 最小数。 /// 最大数。 /// public static int RandomInteger(int argMin, int argMax) { RandomInit(); float vfloat = (argMax - argMin + 1) * RandomFloat(); int vint = argMin + (int)vfloat; return vint; } /// 由指定字符成的随机字符串。 /// 字符池,字符池中每个字符在随机字符串中出现的概率约等。 /// 随机字符串的长度。 public static string RandomCustom(string argPool, int argLength) { switch (TextHelper.Len(argPool)) { case 0: return TextGenerator.Space(argLength); case 1: return TextGenerator.CopyChar(argPool, argLength); default: var vresult = new StringBuilder(); int vl = TextHelper.Len(argPool); string vc; float vn; float vs; Randomize(); while (vresult.Length < argLength) { vn = (vl + 1) * RandomFloat(); vs = Convert.ToInt32(Math.Floor(vn)); if (vs > 0) { if (vs > vl) vs = vl; vc = TextHelper.Mid(argPool, Convert.ToInt32(vs), 1); vresult.Append(vc); } } return vresult.ToString(); } } /// 由易识别字符组成的随机字符串。 /// 随机字符串的长度。 public static string RandomLucid(int argLength) { return TextHelper.LCase(RandomCustom(Constant.LucidCollection, argLength)); } /// 由数字和字母组成的随机字符串。 /// 随机字符串的长度。 public static string RandomSimple(int argLength) { return TextHelper.LCase(RandomCustom(Constant.NumberCollection + Constant.LowerCollection, argLength)); } } }