Skip to content Skip to sidebar Skip to footer

Building A Simple Randomizer Which Includes A Dot, An Underscore, The Letter A And The Letter B

Basically, I want to build a randomizer that can produce an output with following possible combinations: . . . . . . _ _ _ _ _ _ . _ _ . . . _ _ . . . _ _ _ _ . _ . _ . _ . and f

Solution 1:

Here's another way: http://jsfiddle.net/8AHaw/

functionmakeid() {
    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>");

Adapted from Generate random string/characters in JavaScript

Solution 2:

var a = [
    ".",
    ". .",
    ". . .",
    "_",
    "_ _",
    "_ _ _",
    ". _",
    "_ .",
    ". . _",
    "_ . .",
    ". _ _",
    "_ _ .",
    "_ . _",
    ". _ ."
    ],

    b = ["A ", "B "];



    functiongetRandom() {
        return b[Math.random()*b.length|0] + a[Math.random()*a.length|0];
    }

    getRandom(); //"A ."getRandom(); //"A _ _ _"getRandom(); //"B . _ _"

With a loop:

var l = 20;
while(l--) console.log( getRandom() );

Post a Comment for "Building A Simple Randomizer Which Includes A Dot, An Underscore, The Letter A And The Letter B"