I have an array which gets updated at times, so the size of the array is arbitrary.
I would like to get a non-duplicate index from the array (note: not the element at index, but just the index).
The program runs every 1000ms, and I'm trying to get a unique index.
This is what I have so far:
randomIndex = data => {
let rand = Math.floor(Math.random() * data.length);
if (rand !== Math.floor(Math.random() * data.length)) {
return rand;
} else {
return Math.floor(Math.random() * data.length);
}
};
Yet still returns duplciate values
Edit
Suppose we have data = ['bob', 'charli', 'kate']
I would like to get a random index e.i. 0 or 1 or 2.
Because I have setInterval(() => {}, 1000) currently i get the same index after 1000s, which is not what I want. So for every second, I need a different index.
Update
Note that when I say no duplicate, what I really mean is that I do not want the same index to appear consecutively.
So say after 1s we have index 1, then the next 1s after, I want either 0 or 2.
Math.randomcan still return the same value as rand