I am trying to generate a random number from a set of numbers but it does not seem to be working. Basically what I did was I randomly generated a number from a set and kept track of each generated number so that it won’t be generated again.

To try to prevent duplicates from being generated, I tried removing each generated number from the set. I even used a while loop to make sure that no previously generated number is selected for redundancy purposes but it still generates duplicates.

Then once the set is empty, it is set again to the old set of numbers so that I can randomly generate a unique number from it again. Please take a look at my code below and help point out why it is generates duplicate numbers:

```
var set = [0,1,2,3];
var previousNum;
var randNum;
var arrayElementIndex;
document.getElementById('Box').onclick = function() {
// Get a random number from predefined set
randNum = getRndmFromSet(set);
// Get another random number if number was the last chosen number in the set
while(previousNum == randNum){
randNum = getRndmFromSet(set);
}
// record the previously chosen number
previousNum = randNum;
arrayElementIndex = set.indexOf(randNum)
if(set.length > 0){
set.splice(arrayElindex, 1);
}
else {
// Reset the set
set = [0, 1, 2, 3];
randNum = getRndmFromSet(set);
// Get another random number if number was the last chosen number in the set before reset
while(previousNum == randNum){
randNum = getRndmFromSet(set);
}
previousNum = randNum;
arrayElementIndex = set.indexOf(randNum)
set.splice(arrayElementIndex, 1);
}
}
function getRndmFromSet(set) {
var rndm = Math.floor(Math.random() * set.length);
return set[rndm];
}
```