class RandomInfo
{
static Random ran = new Random();
/// <summary>
/// 获取Guid值
/// </summary>
public static Guid GetGuid => Guid.NewGuid();
/// <summary>
/// 获取随机姓名(3个汉字)
/// </summary>
public static string GetName()
{
//.net core使用时需注册下行代码,并向项目导入System.Text.Encoding.CodePages包
//Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding gb = Encoding.GetEncoding("GB2312");
string str = string.Empty;
for (int i = 0; i < 3; i++)
{
// 获取区码(常用汉字的区码范围为16-55)
int regionCode = ran.Next(16, 56);
// 获取位码(位码范围为1-94 由于55区的90,91,92,93,94为空,故将其排除)
int positionCode = ran.Next(1, regionCode == 55 ? 90 : 95);
// 转换区位码为机内码
regionCode += 160;// 160即为十六进制的20H+80H=A0H
positionCode += 160;// 160即为十六进制的20H+80H=A0H
byte[] bytes = new byte[] { (byte)regionCode, (byte)positionCode };
str += gb.GetString(bytes);
}
return str;
}
/// <summary>
/// 获取随机年龄
/// </summary>
public static int GetAge => ran.Next(20, 28);
/// <summary>
/// 获取随机性别
/// </summary>
public static string GetSex => ran.Next(2) % 2 == 0 ? "男" : "女";
/// <summary>
/// 获取日期
/// </summary>
public static string GetDate
{
get
{
DateTime dt = new DateTime(1992, 01, 01);
dt = dt.AddYears(ran.Next(0, 10));
dt = dt.AddMonths(ran.Next(0, 11));
dt = dt.AddDays(ran.Next(0, 32));
return dt.ToString("yyyy-MM-dd");
}
}
/// <summary>
/// 获取随机年级-班级
/// </summary>
public static string GetClassGrade => ran.Next(10).ToString() + "-" + ran.Next(10).ToString();
}
C#获取随机值、汉字
于 2021-04-22 17:03:43 首次发布