dimanche 17 avril 2016

I want to generate 3 random unique numbers from a range of 1 - 8 and store them in an Array javascript

function ques_select() {
    var a = 0;
    var ques_number = new Array(2);
    var i = 0;
    var b = 0;
    for (; i <= 2; i++) {
        ques_number[i] = Math.floor(Math.random() * (9 - 0)) + 0;
        b = i - 1;
        if (ques_number[i] == ques_number[b] && i != 0) {
            i = i - 1;
        }
    }
    for (d = 0; d <= 2; d++) {
        document.getElementById('questions').innerHTML += '<b>' + questionbank[ques_number[d]][0] + '<br/>';
    }
}

I am trying to generate 3 Random Unique numbers from a range of 1 - 8 and I store them in an Array and the print out that Array! For some reason many times 2 of the numbers generated randomly are the same! Please tell me what should I do! Thanks




Aucun commentaire:

Enregistrer un commentaire