const arr = [1, 255, 639, 9, 7, 8, 5, 4, 2];
const task = (arr) => {
const len = arr.length;
let index;
let curr = -1;
let prev = -1;
while (curr === prev) {
index = Math.floor(Math.random() * (len - 1)) + 1;
curr = arr[index];
}
prev = curr;
return curr;
}
const array = [1, 255, 639, 9, 7, 8, 5, 4, 2];
const randomIndex = arr => Math.round(Math.random() * (arr.length-1));
const randomEl = arr => {
let index = randomIndex(arr), prev = arr[index];
return () => {
do {
index = randomIndex(arr);
}
while(prev === arr[index]);
prev = arr[index];
return arr[index];
}
}
const getRandomEl = randomEl(array);
for(let i = 0; i < 10; i++) {
console.log(getRandomEl());
}
function createRandom(arr) {
const uniqArr = [...new Set(arr)]; // выкидываем дубликаты из массива
let size = uniqArr.length;
let index = -1;
return () => {
const rand = Math.floor(Math.random() * size);
index = (rand + index + 1) % uniqArr.length;
size = uniqArr.length - 1;
return uniqArr[index];
};
}
// использование
const getRand = createRandom([0, 1, 2, 3]);
for (let i = 0; i < 15; ++i) {
console.log(getRand());
}