Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to prevent random method from giving more than two same numbers js site:stackoverflow.com

function getNumber() {
    var min = 0,
        max = 4,
        random;

    do {
        random = Math.floor(Math.random() * (max - min)) + min;
    } while (random === getNumber.last);
    getNumber.last = random;
    return random;
};

var i;
for (i = 0; i < 100; i++) {
    console.log(getNumber());
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #prevent #random #method #giving #numbers #js
ADD COMMENT
Topic
Name
5+7 =