Public
Edited
Oct 25, 2023
5 stars
Insert cell
Insert cell
d3.shuffle([0, 1, 2, 3, 4, 5, 6])
Insert cell
Insert cell
Insert cell
{
const array = [0, 1, 2, 3, 4, 5, 6];
d3.shuffle(array);
return array;
}
Insert cell
Insert cell
{
const array = [0, 1, 2, 3, 4, 5, 6];
const shuffled = d3.shuffle(array.slice());
return [array, shuffled];
}
Insert cell
Insert cell
d3.shuffle(Int32Array.of(0, 1, 2, 3, 4, 5, 6))
Insert cell
Insert cell
d3.shuffle([...new Set([0, 1, 2, 3, 4, 5, 6])])
Insert cell
Insert cell
d3.shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 5) // shuffle 5…9
Insert cell
d3.shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 5) // shuffle 0…4
Insert cell
Insert cell
Insert cell
{
const names = ["red", "orange", "yellow", "blue", "green", "violet"];
const colors = ["#ff0000", "#ffa500", "#ffff00", "#0000ff", "#008000", "#ee82ee"];
const order = d3.shuffle(d3.range(names.length));
return {
names: d3.permute(names, order),
colors: d3.permute(colors, order)
};
}
Insert cell
Insert cell
{
const shuffle = d3.shuffler(d3.randomLcg(17912965));
return shuffle([0, 1, 2, 3, 4, 5]);
}
Insert cell
Insert cell
// Note: modified the specified array in-place!
function popRandom(array) {
const n = array.length;
if (!(n >= 1)) return;
const i = Math.floor(Math.random() * n);
const x = array[i];
array[i] = array[n - 1];
array.length = n - 1;
return x;
}
Insert cell
{
const array = d3.range(100);
while (array.length > 0) {
yield popRandom(array);
}
}
Insert cell
Insert cell
Insert cell
d3 = require("d3-array@3", "d3-random@3")
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