Generate random password

Generate random password
This C# function returns a randomly generated password of the specified length. You can easily modify it to include only the characters you are interested in.
1.  private string GenerateRandomPassword(int Length)
2.  {
3.      // We want all popular characters except for the ones that can easily be confused (e.g., l and 1)
4.      string[] Characters = new string[56];
5.      Characters[0] = "a";
6.      Characters[1] = "b";
7.      Characters[2] = "c";
8.      Characters[3] = "d";
9.      Characters[4] = "e";
10.     Characters[5] = "f";
11.     Characters[6] = "g";
12.     Characters[7] = "h";
13.     Characters[8] = "j";
14.     Characters[9] = "k";
15.     Characters[10] = "m";
16.     Characters[11] = "n";
17.     Characters[12] = "p";
18.     Characters[13] = "q";
19.     Characters[14] = "r";
20.     Characters[15] = "s";
21.     Characters[16] = "t";
22.     Characters[17] = "u";
23.     Characters[18] = "v";
24.     Characters[19] = "w";
25.     Characters[20] = "x";
26.     Characters[21] = "y";
27.     Characters[22] = "z";
28.     Characters[23] = "A";
29.     Characters[24] = "B";
30.     Characters[25] = "C";
31.     Characters[26] = "D";
32.     Characters[27] = "E";
33.     Characters[28] = "F";
34.     Characters[29] = "G";
35.     Characters[30] = "H";
36.     Characters[31] = "J";
37.     Characters[32] = "K";
38.     Characters[33] = "L";
39.     Characters[34] = "M";
40.     Characters[35] = "N";
41.     Characters[36] = "P";
42.     Characters[37] = "Q";
43.     Characters[38] = "R";
44.     Characters[39] = "S";
45.     Characters[40] = "T";
46.     Characters[41] = "U";
47.     Characters[42] = "V";
48.     Characters[43] = "W";
49.     Characters[44] = "X";
50.     Characters[45] = "Y";
51.     Characters[46] = "Z";
52.     Characters[47] = "2";
53.     Characters[48] = "3";
54.     Characters[49] = "4";
55.     Characters[50] = "5";
56.     Characters[51] = "6";
57.     Characters[52] = "7";
58.     Characters[53] = "8";
59.     Characters[54] = "9";
60.  
61.     Random RandChar = new Random();
62.     string Password = "";
63.  
64.     for (int i = 0; i < Length; i++)
65.     {
66.         Password += Characters[RandChar.Next(0, 55)];
67.     }
68.  
69.     return Password;
70. }
71.  
72. // How to use it
73. // MessageBox.Show(GenerateRandomPassword(6));
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top