dimanche 6 mars 2016

Pick 15 random numbers put them in an array without getting duplicates

Say i pick 10 random numbers between 1 and 12 and put them into an array. How can i loop through it so it wont have duplicates?

for (var i =0; i < 10; i++) {
        var idx:int=Math.floor(Math.random() * 12) + 1;
        ee.push(idx);

    }

        var r = ee.length;
    while (r<10) {
        if (ee[r] === idx) {
            ee.splice(r,1);

        }else{
            var idx:int=Math.floor(Math.random() * 12) + 1;
            ee.push(idx);
    }
}


    trace(ee);

I have spendt alot of time on this and cant get it to work..




Aucun commentaire:

Enregistrer un commentaire