繁体   English   中英

如何使用 JavaScript 中的给定输入生成完全随机数组的算法?(生成随机表情符号、数字、字符、符号等)

[英]how to make an algorithm that generates totally random arrays with a given input in JavaScript?(generate random emoji, number, character, symbol etc.)

我想知道是否有任何方法可以使用 JavaScript 生成字符串、数字、表情符号、符号等的随机数组。 我的意思是当用户给我们这样的输入时: “gh”“35”“😂👌”无论如何都要创建一个算法来创建每个可能的随机数组,比如: “gh”“hg”“35”“53”“😂👌”“👌😂”

  const createPassword = (characterList) => {
    let password = "";
    const characterListLength = characterList.length;
    for (let i = 0; i < props.passwordLength; i++) {
      const characterIndex = Math.round(Math.random() * characterListLength);
      password = password + characterList.charAt(characterIndex);
    }
    return password;
  };

这是我为密码生成器项目创建的算法,但我对用户给我字符长度和类型的情况感到非常困惑。

该函数看起来像这样。

function random(n){
    int randomval= Math.floor(Math.random()*n);
    return randomval;
}

这里 n 是数组的长度,您可以在其中将所需的所有元素放入字符串中。

这将根据数组长度为您提供数字范围。 然后您可以使用该函数访问“表情符号”或您想要在上面创建的函数中访问的任何字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM