ronaldo aliaga
ronaldo aliaga

Reputation: 1

Need a generator with Java (if possible with a regex)

I need create a generetor with this prefix "sold:" after that i need put 2 words, 1 digit and 1 word.

Example:

I have this but this is for genrate random values.

function makeid() {
  var text = "";
  var possible = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";   //enter the variables*

  for (var i = 0; i < 14; i++)   //change the value depending of the length
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

THANK YOU

Upvotes: 0

Views: 53

Answers (1)

Felix
Felix

Reputation: 2396

Split your problem into small sub-problems.

So, what do we need?

We need a function that gives us a sequence of randomly generated values based on a given set of possible values:

function randomOf(chars, count) {
    var text = "";

    for (var i = 0; i < count; i++) {
        text += chars.charAt(Math.floor(Math.random() * chars.length));
    }

    return text;
}

Then we need a function that gives us a sequence of randomly generated words:

function generateWords(count) {
    return randomOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ", count);
}

And a function that gives us a sequence of randomly generated number(s):

function generateNumbers(count) {
    return randomOf("0123456789", count);
}

Now we can use those functions to generate our id:

function makeid() {
    return "sold:" + generateWords(2) + generateNumbers(1) + generateWords(1);
}

Upvotes: 1

Related Questions