C#随机数字生成的一种方法

时间:2023-03-09 18:42:02
C#随机数字生成的一种方法

1、参考:

   public class RandomLongGenerater
{
public static long New(int bit)
{
if (bit > )
{
throw new Exception("bit must <= 16");
}
if (bit < )
{
throw new Exception("bit must >= 6");
}
string midStr = "";
byte[] bytes = Guid.NewGuid().ToByteArray();
for (int i = ; i < bit; i++)
{
midStr += bytes[i].ToString().Last<char>();
}
if (midStr[] == '')
{
midStr = new Random().Next(, ).ToString() + midStr.Substring();
}
return long.Parse(midStr);
}
public static long New64Bit()
{
return New();
}
public static long New()
{
return New();
} }

2:调用:

RandomLongGenerater.New(16);