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
152 lines
5.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Apewer.Internals
|
|
{
|
|
|
|
internal class RandomHelper
|
|
{
|
|
|
|
/// <summary>生成新的 GUID。</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>用于生成随机数的时钟。</summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>用于生成随机数的时钟位置。</summary>
|
|
private static int RandomSeed = 327680;
|
|
|
|
/// <summary>初始化随机数生成。</summary>
|
|
private static void Randomize()
|
|
{
|
|
RandomInit();
|
|
}
|
|
|
|
/// <summary>初始化随机数生成。</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>初始化随机数生成。</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>生成一个不大于 1 的随机单精度浮点数,最小为 0。</summary>
|
|
public static float RandomFloat()
|
|
{
|
|
return RandomFloat(1f);
|
|
}
|
|
|
|
/// <summary>生成一个随机单精度浮点数。</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>生成随机整数,最小为 0。</summary>
|
|
/// <param name="argMax">最大数。</param>
|
|
/// <returns></returns>
|
|
public static int RandomInteger(int argMax)
|
|
{
|
|
return RandomInteger(0, argMax);
|
|
}
|
|
|
|
/// <summary>生成随机整数。</summary>
|
|
/// <param name="argMin">最小数。</param>
|
|
/// <param name="argMax">最大数。</param>
|
|
/// <returns></returns>
|
|
public static int RandomInteger(int argMin, int argMax)
|
|
{
|
|
RandomInit();
|
|
float vfloat = (argMax - argMin + 1) * RandomFloat();
|
|
int vint = argMin + (int)vfloat;
|
|
return vint;
|
|
}
|
|
|
|
/// <summary>由指定字符成的随机字符串。</summary>
|
|
/// <param name="argPool">字符池,字符池中每个字符在随机字符串中出现的概率约等。</param>
|
|
/// <param name="argLength">随机字符串的长度。</param>
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>由易识别字符组成的随机字符串。</summary>
|
|
/// <param name="argLength">随机字符串的长度。</param>
|
|
public static string RandomLucid(int argLength)
|
|
{
|
|
return TextHelper.LCase(RandomCustom(Constant.LucidCollection, argLength));
|
|
}
|
|
|
|
/// <summary>由数字和字母组成的随机字符串。</summary>
|
|
/// <param name="argLength">随机字符串的长度。</param>
|
|
public static string RandomSimple(int argLength)
|
|
{
|
|
return TextHelper.LCase(RandomCustom(Constant.NumberCollection + Constant.LowerCollection, argLength));
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|