[英]Why does my password generator written in JavaScript often return passwords with repeating characters?
I've written a JavaScript class with four static variables containing the different kinds of characters the final password may include.我写了一个 JavaScript class 和四个 static 变量,包含最终密码可能包含的不同种类的字符。 The class contains four getter functions which return a random character from these four variables.
class 包含四个 getter 函数,它们从这四个变量返回一个随机字符。 In a separate function I try to create the password.
在单独的 function 中,我尝试创建密码。 Unfortunately the final password does not seem to contain wholly random characters.
不幸的是,最终密码似乎并不包含完全随机的字符。 Often same characters are repeated.
通常重复相同的字符。
I had a closer look at my random-function but it seems to be fine.我仔细查看了我的随机函数,但它似乎没问题。 I hope you have an idea why the passwords end up being so similar eg AAAh^8 or bSS+5S
我希望您知道为什么密码最终如此相似,例如 AAAh^8 或 bSS+5S
class Password{
static lowerCase = "abcdefghijklmnopqrstuvwxyz";
static upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static numbers = "0123456789";
static symbols = "!@#$%^&*()_+~\\`|}{[]:;?><,./-=";
length = document.getElementById("passwordLength").value;
getKey = [this.upperCase, this.lowerCase, this.num, this.symbol]
get upperCase(){
return Password.upperCase[Math.floor(Math.random() * Password.upperCase.length)]
}
get lowerCase(){
return Password.lowerCase[Math.floor(Math.random() * Password.lowerCase.length)]
}
get num(){
return Password.numbers[Math.floor(Math.random() * Password.numbers.length)]
}
get symbol(){
return Password.symbols[Math.floor(Math.random() * Password.symbols.length)]
}
password = ""
}
function createPassword(){
const newPass = new Password;
for (var i=0; i<newPass.length; i++){
key = newPass.getKey[Math.floor(Math.random() * newPass.getKey.length)]
newPass.password += key
console.log("Random Key Run " + i + ": " + key)
}
console.log("Final Password: " + newPass.password)
}
createPassword()
As @deceze says, you're essentially pre-selecting your 4 different characters in the getKey
initializer.正如@deceze 所说,您实际上是在
getKey
初始值设定项中预先选择了 4 个不同的字符。
You'll probably have a better time if you're less fancy with getters, eg如果你不那么喜欢吸气剂,你可能会有更好的时间,例如
function pick(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
class PasswordGenerator {
static lowerCase = "abcdefghijklmnopqrstuvwxyz";
static upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static numbers = "0123456789";
static symbols = "!@#$%^&*()_+~\\`|}{[]:;?><,./-=";
static classes = [
PasswordGenerator.lowerCase,
PasswordGenerator.upperCase,
PasswordGenerator.numbers,
PasswordGenerator.symbols,
];
getChar() {
const cls = pick(PasswordGenerator.classes);
return pick(cls);
}
}
function createPassword() {
const generator = new PasswordGenerator();
let password = "";
for (var i = 0; i < 8; i++) {
const key = generator.getChar();
password += key;
console.log(`Random Key Run ${i}: ${key}`);
}
console.log("Final Password: " + password);
}
createPassword();
I also took the liberty of moving stuff that shouldn't necessarily be Password
state out of the PasswordGenerator
class.我还冒昧地将不一定是
Password
state 的内容从PasswordGenerator
生成器 class 中移出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.