1

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;
    }

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?

0

3 Answers 3

5

Move Random r to outside the method if you are repeatedly calling it. You are going to be hitting it several times in the same relative timeframe, so you are going to be generating the same seeds. You also want to get rid of the line below. It is unnecessary, and (again), with the nature of DateTime.Now, you would just continue to generate the same sequence of "random" numbers.

r = new Random((int)(DateTime.Now.Ticks) + i); 
2
  • thats why i added i to it. placing it outside was indeed the best option.
    – Stefanvds
    Sep 28, 2010 at 17:15
  • Yes, but as an explanation, adding i still results in the same seeds and sequences. Consider that DateTime.Now would be unchanged through several method calls. Let's say, for example, that DateTime.Now.Ticks was 0 at the time of the first method call for the sake of argument. For the first several executions of this method, it will happen so fast that DateTime.Now has not updated to a fresh value (see the blog article). So your loop inside the method would always go through 0, 1, 2, 3, 4, etc. Same seeds for each execution of the method, same sequence produced by the Random object. Sep 28, 2010 at 17:24
2

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

How can I get true randomness in this class without Thread.Sleep(300)?

1
  • 1
    you told me HOW to place it outside the method neatly with a static. thanks.
    – Stefanvds
    Sep 28, 2010 at 17:15
0

Use a set rather than whatever collection you store into and don't loop 100 times but until the set has 100 items in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.