Standing on the shoulders of giants. RSS 2.0
# Thursday, October 13, 2005

public static string GeneratePassword(int length) {

string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?";

Random rnd = new Random( (int)DateTime.Now.Ticks );
            
char[] chars = new char[length];


for(int i = 0;i<length;i++) {
chars[i] = allowedChars[ rnd.Next( allowedChars.Length ) ];
}

return new string(chars);
}

When running in a tight loop, you're better of pulling the initialisation of the randomGenerator outside the method, or not use DateTime.Now.TickCount as the seed.

Thursday, October 13, 2005 3:12:54 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
Codesnippet
Tracked by:
"Random Password Generator" (jazmynec) [Trackback]
Comments are closed.
About
© Copyright 2008
Paul van Brenk
Sign In
newtelligence dasBlog 2.3.8275.16006
All Content © 2008, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)