0

Some folks helped me build a randomizer:

function makeid() {
    var text = "";
    var possibleChars = "._";
    var possibleLetters = "AB";

    text += possibleLetters.charAt(Math.floor(Math.random() * possibleLetters.length));
    for( var i=0; i < Math.floor(Math.random() * 3)+1; i++ )
        text += " " + possibleChars.charAt(Math.floor(Math.random() * possibleChars.length));

    return text;
}

for (var i=0; i < 20; i++)
    $("body").append("<div>" + makeid() + "</div>");

It outputs something like this:

B . .

B .

B _ _

A _ .

B .

A .

A _

B . .

A _

A _

B _ .

B . . .

B _

B _ .

I want to modify the JavaScript code above so that A and B don't repeat themselves more than twice in a row:

A . .

B .

B _ _

A _ .

B .

A .

A _

B . .

A _

A _

B _ .

B . . .

A _

B _ .

Any suggestions?

Community
  • 1
  • 1
alexchenco
  • 53,565
  • 76
  • 241
  • 413

3 Answers3

1

Remember two last generated letters (or, even better: amount of recently generated same letters in a row - it'd be easier to adapt to any other number). If new one is about to be same, insert another. Done.

Radomness will suffer.

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • 1
    Specifically: if your output set is really just 2 choices, then after 2 equal values the next one will be entirely predictable (i.e. not random at all!). – Joachim Sauer Jun 29 '12 at 10:43
1

Here you go - not pretty, but should work (alas, not tested):

function makeid() {
    var text = "";
    var possibleChars = "._";
    var possibleLetters = "AB";

    while ( true ) {
        var ch= possibleLetters.charAt(Math.floor(Math.random() * possibleLetters.length));
        if ( this._prev1 != this._prev2 || this._prev1 != ch ) break;
    }
    this._prev2= this._prev1;
    this._prev1= ch;
    text += ch;

    for( var i=0; i < Math.floor(Math.random() * 3)+1; i++ )
        text += " " + possibleChars.charAt(Math.floor(Math.random() * possibleChars.length));

    return text;
}
Searle
  • 957
  • 2
  • 7
  • 19
  • Hey it worked! Thanks. So is this randomness or not? After reading the guys above I'm not sure anymore. – alexchenco Jun 29 '12 at 10:58
  • As soon as you filter certain results, you loose randomness. In this case, when I pick two IDs beginning with A, the next ID isn't completely random, because I know it must begin with B. But it's more a question of whether it's random _enough_ for your application. – Searle Jun 29 '12 at 11:17
  • OK, thanks. Yeah I guess its random enough (Not sure if it is a philosophical or mathematical question). – alexchenco Jun 29 '12 at 11:23
0

The way to generate a random sample from a distribution depends on the distribution, of course. If you don't care about that you could simply keep track of the last two letters generated and discard the newly generated ones if they don't satisfy the constraint.

If you would like to treat this as a second order Markov model, though, it's better to define the probabilities of every letter given the two preceding ones, something like P(A|AA)=0; P(B|AA)=1; P(A|BB)=1; P(B|BB)=0; P(A|AB)=0.5; P(B|AB)=0.5; etc. Note that if all the conditional distributions are uniform, save for the ones that cause the same symbol to appear thrice (which are also uniform over the reduced set of outcomes), this will yield the same distribution as the constrained generation procedure above.

Qnan
  • 3,714
  • 18
  • 15