简体   繁体   English

如何创建一个 aphabets 数组,从中提取随机的大写和小写字母?

[英]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.

相关问题 如何使我的搜索表单使用大写和小写 - How can I make my search form work with uppercase and lowercase 如何使用 JavaScript 测试字符串中的字母是大写还是小写? - How can I test if a letter in a string is uppercase or lowercase using JavaScript? 如何使用 javaScript 创建一个 function (countMs),它采用字符串文本并返回其中的字母数量(大写和小写) - how can I Create a function (countMs) that takes a string text and returns the number of letters'm'in it(both uppercase and lowercase)using javaScript 如何在 JavaScript 中为一组字母(小写和大写)实现排列生成器(或记忆函数)? - How to implement a Permutations generator(or memoized function) for set of Alphabets(both lowercase and uppercase) in JavaScript? 如何创建一个带有随机数的数组? - how can I create an array with random numbers? 我如何让我的正则表达式用附加变量将小写字母替换为大写字母 - How do i get my regex to replace lowercase to uppercase with additional variables 如何让字符串以小写字母开头? - how can i get the string start with lowercase? 如何在 Javascript 中获取字符串的大写字母? - How can I get uppercase of a string in Javascript? 如何根据从字符串中提取的名称在运行时创建变量? - How can I create variables at run-time based on names pulled from a string? 我如何使用 Joi 有效功能并允许小写和大写字母 - How can i use Joi valid function and allow both lowercase and uppercase letters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM