【问题标题】:How to create random string in Javascript? [duplicate]如何在Javascript中创建随机字符串? [复制]
【发布时间】:2017-10-10 14:04:40
【问题描述】:

我想创建random 字符串。但我没有得到正确的方法。谁能帮帮我?

我的尝试:

var anysize = 3;//the size of string 
var charset = "abcdefghijklmnopqrstuvwxyz"; //from where to create
console.log( Math.random( charset ) * anysize ); //getting bad result

可以纠正我吗?或任何其他优雅的方法来解决这个问题?

提前致谢。

【问题讨论】:

标签: javascript


【解决方案1】:

您要做的第一件事是创建一个辅助函数,该函数可以从数组中获取随机值。

getRandomValue(array) {
   const min = 0; // an integer
   const max = array.length; // guaranteed to be an integer

   /*
    Math.random() will return a random number [0, 1) Notice here that it does not include 1 itself
    So basically it is from 0 to .9999999999999999

    We multiply this random number by the difference between max and min (max - min). Here our min is always 0.
    so now we are basically getting a value from 0 to just less than array.length
    BUT we then call Math.floor on this function which returns the given number rounded down to the nearest integer
    So Math.floor(Math.random() * (max - min)) returns 0 to array.length - 1
    This gives us a random index in the array
   */
   const randomIndex = Math.floor(Math.random() * (max - min)) + min;

   // then we grab the item that is located at that random index and return it
   return array[randomIndex];
}

你可以使用这个辅助函数而不用考虑改变字符串的长度,像这样:

var randomString = getRandomValue(charset) + getRandomValue(charset) + getRandomValue(charset);

但是,您可能希望根据您希望随机字符串的长度创建另一个包含循环的函数:

function getRandomString(charset, length) {
  var result = '';
  for (var i = 0; i <= length; i++) {
    result += getRandomValue(charset);
  }
  return result;
}

这个函数会这样使用

var randomString = getRandomString(charset, 3);

【讨论】:

    【解决方案2】:

    您可以获取字符串charset 的n-index char 并根据需要多次附加到新字符串,请参见以下内容:

    var anysize = 3;//the size of string 
    var charset = "abcdefghijklmnopqrstuvwxyz"; //from where to create
    var i=0, ret='';
    while(i++<anysize)
      ret += charset.charAt(Math.random() * charset.length)
      
    console.log(ret);

    【讨论】:

      【解决方案3】:
      function randomString(anysize, charset) {
          var res = '';
          while (anysize--) res += charset[Math.random() * charset.length | 0];
          return res;
      }
      

      类似的东西

      【讨论】:

        【解决方案4】:

        您应该使用可能的字符字符串的.length 属性(charset)。

        另外,使用Math.floor 方法来获取您的chars 数组的integer 位置。

        您可以使用数组indexcharset 字符串中获取随机项:

        charset[Math.floor(Math.random() * charset.length)]
        

        var anysize = 3;//the size of string 
        var charset = "abcdefghijklmnopqrstuvwxyz"; //from where to create
        result="";
        for( var i=0; i < anysize; i++ )
                result += charset[Math.floor(Math.random() * charset.length)];
        console.log(result);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-06-02
          • 1970-01-01
          • 1970-01-01
          • 2012-07-20
          • 2013-04-12
          • 1970-01-01
          • 2010-10-01
          相关资源
          最近更新 更多