0

I would like to create random strings. But I am not get a right way. any one can help me please?

my try :

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

Is it possible to correct me? or any other elegant way to fix this?

Thanks in advance.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

4 Answers4

3

You should use .length property of your string of possible characters(charset).

Also, use Math.floor method in order to get integer positions of your chars array.

You can get a random item from charset string using its array index:

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);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
3
function randomString(anysize, charset) {
    var res = '';
    while (anysize--) res += charset[Math.random() * charset.length | 0];
    return res;
}

Something like that

Oleg Pnk
  • 332
  • 2
  • 5
2

You could get the n-index char of string charset and append to a new string many times as you need, see following please:

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);
Alessandro
  • 4,382
  • 8
  • 36
  • 70
1

The first thing you will want to do is create a helper function that can grab a random value from an array.

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];
}

You could use this helper function with no regard for changing the length of the string, like this:

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

However, you may want to create another function that contains a loop based on how long you want the random string to be:

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

And that function would be used like this

var randomString = getRandomString(charset, 3);

adam-beck
  • 5,659
  • 5
  • 20
  • 34