Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  194] [ 5]  / answers: 1 / hits: 185871  / 15 Years ago, wed, september 30, 2009, 12:00:00

What would be the best approach to creating a 8 character random password containing a-z, A-Z and 0-9?



Absolutely no security issues, this is merely for prototyping, I just want data that looks realistic.



I was thinking a for (0 to 7) Math.random to produce ASCII codes and convert them to characters. Do you have any other suggestions?


More From » javascript

 Answers
16

I would probably use something like this:



function generatePassword() {
var length = 8,
charset = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,
retVal = ;
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
}


That can then be extended to have the length and charset passed by a parameter.


[#98590] Friday, September 25, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarettajb

Total Points: 678
Total Questions: 94
Total Answers: 90

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;