Public
Edited
Jun 2, 2023
Insert cell
Insert cell
function getNUniqueRandomNumbers(n, max) {
var arr = [];
while (arr.length < n) {
const r = Math.floor(Math.random() * max);
if (arr.indexOf(r) === -1) {
arr.push(r);
}
}
return arr;
}
Insert cell
function getUniqueRandomFromArray(arr) {
const indices = getNUniqueRandomNumbers(3, arr.length);
console.log({ arr, indices })
return indices.map(i => arr[i]);
}
Insert cell
getUniqueRandomFromArray(['test', 'test2', 'test3', 'test4'])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more