-1

How do i generate 1 - 10 unique numbers, I did this

function getRandomArbitrary(min, max) {
    var number = Math.floor(Math.random() * (max - min) + min);
    return number;
}

but this sometime return duplicate numbers i want all the number to go around once before they apear again

Waqar Haider
  • 929
  • 10
  • 33

1 Answers1

2

Try the following:

function shuffle(arra1) {
    var ctr = arra1.length, temp, index;

    while (ctr > 0) {

        index = Math.floor(Math.random() * ctr);
        ctr--;
        temp = arra1[ctr];
        arra1[ctr] = arra1[index];
        arra1[index] = temp;
    }
    return arra1;
}
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9,10];
console.log(shuffle(myArray));
amrender singh
  • 7,949
  • 3
  • 22
  • 28