In my dataset, which is a 2D matrix, here is an example:
data = [["a", "b", "c", "d"], ["e", "g"], ["i", "j", "k"]]
I am looking to retrieve N random (x, y)
indexes without any duplicates.
Previously, I had a similar question and here is the solution I found to select 2 sets of x, y combinations:
const data = [["a", "b", "c", "d"], ["e", "g"], ["i", "j", "k"]];
function combinations(data) {
const i11 = Math.floor(Math.random() * data.length);
const i12 = Math.floor(Math.random() * data[i11].length);
const dataLength = data[i11].length > 1 ? data.length : data.length - 1;
let i21 = Math.floor(Math.random() * dataLength);
if (i21 >= i11 && data[i11].length === 1) ++i21;
const innerDataLength = i21 === i11 ? data[i21].length - 1 : data[i21].length;
let i22 = Math.floor(Math.random() * innerDataLength);
if (i21 === i11 && i22 >= i12) ++i22;
return [[i11, i12], [i21, i22]];
}
console.log(combinations(data));
for (let i = 0; i < 10000; ++i) {
const [[i11, i12], [i21, i22]] = combinations(data);
if (i11 === i21 && i12 == i22) console.log('Test failed!');
}