Click here to Skip to main content
15,911,891 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello,

I want again, to randomise my password[] array.
I tried but it is not working, how do I do it?


C#
static string MakePassword1(int pl)
        {                       
            char[] comb = new char[pl];
            string possibles = "ZYXWVUTSRQPNMLKJHGFEDCBA";
            char[] passwords = new char[pl];
            Random rd = new Random();
            string possibles1 = "0123456789";
            Random rd1 = new Random();
            string possibles2 = "abcdefghjkmnpqrstuvwxyz";
            
            Random rd2 = new Random();
            string possibles3 = "!@#$%^&*(?)_+?|~";
            Random rd3 = new Random();

            try
            {
                for (int k = 0; k <= pl; k = k + 4)
                {

                    passwords[k] = possibles[rd.Next(0, possibles.Length)];
                    passwords[k + 1] = possibles1[rd1.Next(0, possibles1.Length)];
                    passwords[k + 2] = possibles2[rd2.Next(0, possibles2.Length)];
                    passwords[k + 3] = possibles3[rd3.Next(0, possibles3.Length)];                  

                }
            }

            catch (IndexOutOfRangeException)
            {
                
                return new string(passwords);
            }
                   

            return new string(passwords);
        }
Posted
Updated 13-Mar-11 22:24pm
v4
Comments
Dalek Dave 14-Mar-11 4:24am    
Edited for Clarity.

 
Share this answer
 
Comments
Dalek Dave 14-Mar-11 4:24am    
Good Links.
Abhinav S 14-Mar-11 4:32am    
Thanks DD.
Sergey Alexandrovich Kryukov 14-Mar-11 12:39pm    
Agree, my 5
--SA
Abhinav S 15-Mar-11 0:13am    
Thank you.
Code is pretty bad. Put all your "possibles" in one string. Do not catch exception! This is pointless and will give you a nightmare when you debug code. It it it possible to define possibles just in the form of range (two variables: minimum and maximum code point), would be even easier, as your would simply convert a randomly-generated integer to a character, not using indexes, which is better for performance as you would eliminate exactly one step of indirection.

(See my directions on using exceptions here:
How do i make a loop that will stop when a scrollbar reaches the bottom[^]
When i run an application an exception is caught how to handle this?[^])

In the loop, use 0 to <length, not <=length, use k++.

Use longer variable names, never of one character. Imagine how you do the search. Don't use names ending with 1, 2, etc., rename all auto-generated names to somethings semantic. Use Microsoft naming conventions.

Use only one instance of Random, initialize it outside of your method, only once per application run-time.

Use Random.Next with one parameter: maxValue, because it's always an index. Don't make a mistake in maxValue: it's not String.Length but String.Length - 1.

—SA
 
Share this answer
 
v2
Comments
Dalek Dave 14-Mar-11 4:24am    
Good answer, gets a 5.
Sergey Alexandrovich Kryukov 14-Mar-11 12:38pm    
Thank you, Dalek.
--SA
Abhinav S 14-Mar-11 4:32am    
Good comments. 5.
Sergey Alexandrovich Kryukov 14-Mar-11 12:38pm    
Thank you, Abhinav.
--SA
I would advise that instead of having several strings of 'Possibles', you set up a single array of possibles.
Then you can generate from that randomly.
It requires much less manipulation.
 
Share this answer
 
Comments
Abhinav S 14-Mar-11 4:33am    
Good comment. 5.
Sergey Alexandrovich Kryukov 14-Mar-11 12:38pm    
Correct, but I gave this advice an hour before, see :-)
--SA
Dalek Dave 14-Mar-11 12:53pm    
Ah, but I suggested an Array approach :)
Sergey Alexandrovich Kryukov 14-Mar-11 17:56pm    
If this is array of strings, this is not a good advice. Should be either one single string or array of characters which is essentially the same thing. If possible, just the range will be event better (I'll update my answer).
Sergey Alexandrovich Kryukov 14-Mar-11 18:00pm    
My Answer upgraded with the possibility of range.
--SA
http://www.obviex.com/Samples/Password.aspx[^]

Check this your question gets solved.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900