// перемешивает массив случайным образом
const shuffle = arr => {
let currentIndex = arr.length, randomIndex;
while (currentIndex > 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[arr[currentIndex], arr[randomIndex]] = [arr[randomIndex], arr[currentIndex]];
}
return arr;
};
const getRandomPairs = (arrA, arrB, n) => {
const [lengthA, lengthB] = [arrA.length, arrB.length];
const indexPairs = [];
for (let a = 0; a < lengthA; a++) {
for (let b = 0; b < lengthB; b++) {
indexPairs.push([a, b]);
}
}
shuffle(indexPairs);
indexPairs.length = Math.min(n, indexPairs.length);
return indexPairs.map(pair => [arrA[pair[0]], arrB[pair[1]]].join(' '));
};
// использование
const NAMES = ['Иван', 'Marya', 'Gendalf', 'Rick'];
const SURNAMES = ['Sus', 'Ger', 'Gray', 'Graims'];
getRandomPairs(NAMES, SURNAMES, 8);
// [ "Rick Graims", "Marya Sus", "Gendalf Gray", "Gendalf Graims", "Gendalf Sus", "Marya Gray", "Иван Gray", "Rick Sus" ]