[英]How can I create an aphabets array from which random uppercase & lowercase alphabets get pulled?
I need to create an array from which random uppercase and lowercase alphabets can be used to generate a password.我需要创建一个数组,从中可以使用随机的大写和小写字母来生成密码。 Could you tell me how to create such an array and add Math.random() to it?你能告诉我如何创建这样一个数组并将 Math.random() 添加到其中吗?
Users Marius and Mina commented on your question suggesting the answers from this post and this post -- which are valid and versatile answers.用户 Marius 和 Mina 对您的问题发表了评论,提出了这篇文章和这篇文章的答案——这些答案是有效且通用的答案。 As an alternative, the following function returns a string of a fixed length containing random uppercase and lowercase letters -- without the use of a long string literal containing the alphabet:作为替代方案,以下函数返回一个包含随机大写和小写字母的固定长度字符串——不使用包含字母的长字符串文字:
function generateCode(length) { var code = ''; for(var i = 0; i < length; i++) { var character = String.fromCharCode(Math.floor(Math.random()*26)+97); if(Math.floor(Math.random()*2) == 1) { character = character.toUpperCase(); } code+= character; } return code; } console.log(generateCode(12));
Hopefully this is useful to you, although the passcode it generates may not be particularly secure as a password.希望这对您有用,尽管它生成的密码作为密码可能不是特别安全。 The questions linked above have additional comments and responses which contain more resources and answers to your question.上面链接的问题有额外的评论和回复,其中包含更多资源和对您问题的回答。 Their answers may generate passwords with more security and randomness.他们的回答可能会生成更安全和随机的密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.