简体   繁体   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.)

i was wondering if there is any way to generate a random array of strings, numbers, emojis, symbols etc. with JavaScript.我想知道是否有任何方法可以使用 JavaScript 生成字符串、数字、表情符号、符号等的随机数组。 what do i mean by that is when user give us an input like: "gh" or "35" or "😂👌" is there anyway to create an algorithm to create every possible random array, like: "gh" and "hg" , "35" and "53" , "😂👌" and "👌😂" ?我的意思是当用户给我们这样的输入时: “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;
  };

This is the algorithm I created for the password generator project but I'm so confused with the case when the user gives me the character length and type.这是我为密码生成器项目创建的算法,但我对用户给我字符长度和类型的情况感到非常困惑。

the function will look something like this.该函数看起来像这样。

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

here n is the length of the array in which you can put all the elements you want in your string.这里 n 是数组的长度,您可以在其中将所需的所有元素放入字符串中。

this will give you the range of numbers according to the array length.这将根据数组长度为您提供数字范围。 then you can use the function to access the 'emoji' or any string you want to access in the function which you have created above.然后您可以使用该函数访问“表情符号”或您想要在上面创建的函数中访问的任何字符串。

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

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