Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
hello, Im using this code for password generation, but i want unique character password. character must be unique in a password.plz help me


C#
private static string MakePassword(int pl) 
       {
           char[] comb = new char[pl];
           string possibles =
           "ZYXWVUTSRQPONMLKJIHGFEDCAB";
           char[] passwords = new char[pl];
           Random rd = new Random();
         
           try
           {


               char ss = possibles[rd.Next(0, possibles.Length)];
              char sp= possibles[rd.Next(0, possibles.Length)];
              if (ss != sp)
              {
                  char a = sp;
              }
               passwords[1] = possibles[rd.Next(0, possibles.Length)];
               passwords[2] = possibles[rd.Next(0, possibles.Length)];
               passwords[3] = possibles[rd.Next(0, possibles.Length)];
               passwords[4] = possibles[rd.Next(0, possibles.Length)];
               passwords[5] = possibles[rd.Next(0, possibles.Length)];
               passwords[6] = possibles[rd.Next(0, possibles.Length)];
               passwords[7] = possibles[rd.Next(0, possibles.Length)];
               passwords[8] = possibles[rd.Next(0, possibles.Length)];
               passwords[9] = possibles[rd.Next(0, possibles.Length)];
               passwords[10] = possibles[rd.Next(0, possibles.Length)];
               passwords[11] = possibles[rd.Next(0, possibles.Length)];
               passwords[12] = possibles[rd.Next(0, possibles.Length)];
               passwords[13] = possibles[rd.Next(0, possibles.Length)];
               passwords[14] = possibles[rd.Next(0, possibles.Length)];
               passwords[15] = possibles[rd.Next(0, possibles.Length)];
               passwords[16] = possibles[rd.Next(0, possibles.Length)];
               passwords[17] = possibles[rd.Next(0, possibles.Length)];
               passwords[18] = possibles[rd.Next(0, possibles.Length)];
               passwords[19] = possibles[rd.Next(0, possibles.Length)];
               passwords[20] = possibles[rd.Next(0, possibles.Length)];
               passwords[21] = possibles[rd.Next(0, possibles.Length)];
               passwords[22] = possibles[rd.Next(0, possibles.Length)];
               passwords[23] = possibles[rd.Next(0, possibles.Length)];
           }

           catch (IndexOutOfRangeException e)
           {
               for (int j = 0; j < pl; j++)
               {
                   comb[j] = passwords[j];
               }
           }
           return new string(comb);
       }
Posted
Updated 10-Mar-11 22:44pm
v3
Comments
Dalek Dave 11-Mar-11 4:43am    
Edited for Code block
[no name] 11-Mar-11 4:45am    
Edited for Grammar.
Dima Popov 11-Mar-11 5:04am    
The try/catch block can be omitted.

One a character has been selected, it needs to be removed from the list of remaining available characters.
I would suggest you have an string array 0 to 25, apply the random factor, and, remove. Test to see if that figure has been selected before, if so, run the random again, and if not, select it.

This is fairly simple stuff, but try it, and if you run into difficulties we can help you further.
 
Share this answer
 
v2
Comments
Espen Harlinn 11-Mar-11 4:48am    
Good suggestion :)
situ21 11-Mar-11 5:19am    
Im unable to remove,plese help me I do
Albin Abel 11-Mar-11 7:11am    
My 5
Have a look at this article

A C# Password Generator[^]

This has certain fields that let you control the password generation

* Exclusions: Specifies the set of characters to exclude in password generation.
* Minimum: Specifies the minimum length of the generated password.
* Maximum: Specifies the maximum length of the generated password.
* ConsecutiveCharacters: Controls generation of consecutive characters in the generated password.
* RepeatingCharacters: Controls generation of repeating characters in the generated password.
* ExcludeSymbols: Excludes symbols from the set of characters used to generate the password


Use of the ConsecutiveCharacters and RepeatingCharacters properties should let you achieve what you require
 
Share this answer
 
Comments
Espen Harlinn 11-Mar-11 11:51am    
Nice link, my 5
I have only one advice: stop generating passwords; there is no use in any passwords for you is you allow yourself to repeat passwords[1] = possibles[rd.Next(0, possibles.Length)] 23 times.
Have you ever heard of the DRY principle, http://en.wikipedia.org/wiki/DRY[^], maybe #1 in programming?

Seriously, learn what are loops, exceptions (methods, parameters, variables).
The fact there is a loop in your code doesn't mean you have an idea what is it for.

Read this:
I have a problem with my program. Please help![^]

And this is for some sobering:
http://norvig.com/21-days.html[^].

Good luck and best wishes,
—SA
 
Share this answer
 
v2

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