产生一批随机密码 [英] generating a batch of random passwords

查看:106
本文介绍了产生一批随机密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成随机密码是容易的。但产生了一批比较困难。

Generating a random password is easy. but generating a batch is more difficult.

    public static string getRandomPassword(int letters, int getallen) {
        //int letters = 8;
        //int getallen = 5;

        char[] letterdeel = new char[letters];
        int minGetal = (int)Math.Pow(10, getallen - 1);
        int maxGetal = (int)Math.Pow(10, getallen);

        string password;
        Random r = new Random();
        int test = (int)(DateTime.Now.Ticks);
        for (int i = 0; i < letters; i++) {
            r = new Random((int)(DateTime.Now.Ticks) + i);
            bool capital = r.Next(2) == 0 ? true : false;
            if (capital) {
                letterdeel[i] = (char)r.Next(65, 91);
            } else {
                letterdeel[i] = (char)r.Next(97, 123);
            }
        }

        password = new string(letterdeel);
        password += r.Next(minGetal, maxGetal);

        return password;
    }

这是我的方法,密码应该是在一定的字母数字格式。
这工作正常,但是如果我有一个循环从该方法获取密码100,在我的阵列我有5-8相同的密码,然后再5 -8相同的密码。

this is my method, the passwords should be in a certain letter-number format. this works fine, however if i have a for loop pulling 100 passwords from this method, in my array i have 5-8 the same passwords, then again 5-8 the same pass.

我知道这是为什么,因为随机函数,它依赖于时钟的,但我怎么解决这个问题?

i know WHY this is, because of the random function and the clock it depends on, but how do i fix this?

推荐答案

定义你的随机数发生器作为函数的静态之外。

Define your random number generator as a static outside of the function.

< A HREF =http://stackoverflow.com/questions/3660288/how-can-i-get-true-randomness-in-this-class-without-thread-sleep300/3660321#3660321>的http://计算器.COM /问题/ 3660288 /如何-可以-I-得到真随机性,在这个类且不线程sleep300 / 3660321#3660321

这篇关于产生一批随机密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆