随机生成N个由数字和小写字母组成的字符窜

public class Consts
{
    /// <summary>
    /// 26个小写的英文字符
    /// </summary>
    public static readonly string[] strsLeter = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

    /// <summary>
    /// 0,1,2,3,4,5,6,7,8,9
    /// </summary>
    public static readonly string[] strsNum = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
}


public class Utill
{
/// <summary>
    /// 随机生成N个字符
    /// </summary>
    /// <param name="size">参数长度介于6和50之间</param>
    /// <returns></returns>
    public static string CreateRandomStr(int size)
    {
        if (size < 6 || size > 50)
            return Consts.strUPwd;
        //字母个数
        int leter = size / 2;
        //数字个数
        int num = size - leter;


        Random ran = new Random();
        string pwd = string.Empty;
        int index = -1;
        //生成随机的字母
        for (int i = 0; i < leter; i++)
        {
            index = ran.Next(0, Consts.strsLeter.Length - 1);
            pwd += Consts.strsLeter[index];
        }
        //生成随机的数字
        for (int i = 0; i < num; i++)
        {
            index = ran.Next(0, Consts.strsNum.Length - 1);
            pwd += Consts.strsNum[index];
        }
        //打乱字母和数字的顺序
        string tmpPwd = pwd;
        pwd = string.Empty;
        while (tmpPwd.Trim().Length > 0)
        {
            index = ran.Next(0, tmpPwd.Length - 1);
            pwd += tmpPwd[index];
            tmpPwd=tmpPwd.Remove(index, 1);
        }
        return pwd;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值