0

I am generating random strings using the below function in node.js. I wanted to know if there is any way to create text strings appropriately with a common string within every randomly generated string.

EDIT: The common string can be in any location of the generated string

For example:

Randomly generated string - Cxqtooxyy4

Can I add 'abc' or 'ABC' within that string like this - Cxqtoabcoxyy4 or CxqtoABCoxyy4 respectively.

My Code -

var randomTextArrayGeneration = function(size)
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for(var i=0;i<size;i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    return text;
}

Can anyone tell me how do I do this? Any help is really helpful.

7
  • Should the common string always be at the same position within the random string? What if the size specified is shorter than the common string? Commented May 28, 2016 at 21:53
  • A little unclear about your requirements. Does the embedded string need to be in the same location each time? Does it need to be in a random location each time? Does it not matter where it is? Commented May 28, 2016 at 21:53
  • Sorry didn't explain it properly. The embedded string can be in any location. Commented May 28, 2016 at 21:54
  • 1
    Can you...yes, of course. Shouldn't be hard to get an idea how by looking at the various substring/manipulation methods that are documented in numerous places on web and at least making an attempt Commented May 28, 2016 at 21:54
  • 1
    Any location? Then why not just place it at the end after generating a random string with length size -3 (assuming the fixed string is of length 3). Commented May 28, 2016 at 21:56

3 Answers 3

1
var n = text.length; //The size of your random string
var randomPosition = Math.floor((Math.random() * n) + 1); //Generate a  random number between 1 and the size of your string

//Separate your string in 2 strings
var text1 = text.substring(1, randomPosition); 
var text2 = text.substring(randomPosition, n); 

//Create your final string by adding the common string between your two halves
var textFinal = text1 + commonString + text2;

return textFinal;

I don't remember how exactly works .substring(), you may want to change 1 by 0 in some places.

Sign up to request clarification or add additional context in comments.

Comments

1

A rough sketch of the algorithm is this:

  1. create random string of length size - <FIXED_STRING>.length
  2. append <FIXED_STRING> to the end of generated string

Done.

A corner case is if size < <FIXED_STRING>.length, here you would need to provide some more discussion on what should happen.

1 Comment

Thank you for the answer :) It gave me an idea I could try it on my own as well :)
1

You can use String.prototype.slice() to select 0-n characters from possible to insert into random index within string returned from randomTextArrayGeneration. If 0 is passed to randomTextArrayGeneration the selected string from possible will be set as result

var randomTextArrayGeneration = function(size, from, to) {
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
      for (var i = 0; i < size; i++) {
        text += possible.charAt(Math.floor(Math.random() * possible.length))
      };
      var len = Math.floor(Math.random() * text.length - 3);
      var res = text.slice(0, len) + possible.slice(from, to).toLowerCase() + text.slice(len);
      return res
}

1 Comment

Thank you for the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.