生成随机字符串

来源:互联网 发布:淘宝上的考试作弊神器 编辑:程序博客网 时间:2024/04/25 02:14
 

private string RandomNum(int Num)
    {
        string allchar = "0,1,2,3,4,5,6,7,8,9";
        string[] allArray = allchar.Split(',');//拆分成数组
        string randomNum = "";

        int temp = -1;//记录上次随机数的数值,尽量避免产生几个相同的随机数

        Random random = new Random();

        for (int i = 0; i < Num; i++)
        {
            if (temp != -1)
            {
                random = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int n = random.Next(9);
            if (temp == n)
            {
                return RandomNum(Num);
            }
            temp = n;
            randomNum += allArray[n];

        }
        return randomNum;
    }

原创粉丝点击