C#生成随机数、字符串、订单号

/// 
/// 生成随机数字
/// 
/// 生成长度
/// 
public static string Number(int Length)
{
     
    return Number(Length, false);
}
/// 
/// 生成随机数字
/// 
/// 生成长度
/// 是否要在生成前将当前线程阻止以避免重复
/// 
public static string Number(int Length, bool Sleep)
{
     
    if (Sleep)
        System.Threading.Thread.Sleep(3);
    string result = "";
    Random random = new Random();
    for (int i = 0; i < Length; i++)
    {
     
        result += random.Next(10).ToString();
    }
    return result;
}
/// 
/// 生成随机字母字符串(数字字母混和)
/// 
/// 待生成的位数
public static string GetCheckCode(int codeCount)
{
     
    string str = string.Empty;
    int rep = 0;
    long num2 = DateTime.Now.Ticks + rep;
    rep++;
    Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
    for (int i = 0; i < codeCount; i++)
    {
     
        char ch;
        int num = random.Next();
        if ((num % 2) == 0)
            ch = (char)(0x30 + ((ushort)(num % 10)));
        else
            ch = (char)(0x41 + ((ushort)(num % 0x1a)));
        str = str + ch.ToString();
    }
    return str;
}
/// 
/// 根据日期和随机码生成订单号
/// 
/// 
public static string GetOrderNumber()
{
     
    string num = DateTime.Now.ToString("yyMMddHHmmss");//格式化日期yyyyMMddHHmmssffff
    return num + Number(4, true).ToString();
}

你可能感兴趣的:(C#,C#,生成随机数,生成随机字符串,生成订单号)