Published
Edited
Feb 22, 2019
7 stars
Insert cell
Insert cell
ShuffleVec = {
function ShuffleVec(data){
this.data = data || [];
if (this.data.length) this.shuffle();
};
ShuffleVec.prototype.shuffle = function() {
const data = this.data;
for(let i = 0; i < data.length; i++){
const j = Math.random()*i | 0;
const v = data[j];
data[j] = data[i];
data[i] = v;
}
};
ShuffleVec.prototype.push = function(val) {
const data = this.data;
// insert new value
data.push(val);
// do a random swap
const l = data.length;
const i = Math.random()*l | 0;
data[l-1] = data[i];
data[i] = val;
return l;
};
ShuffleVec.prototype.pop = function() {
return this.data.pop();
};
ShuffleVec.prototype.unshift = function(val) {
const data = this.data
// insert new value
data.unshift(val);
// do a random swap
const l = data.length;
const i = Math.random()*l | 0;
data[0] = data[i];
data[i] = val;
return l;
};
ShuffleVec.prototype.shift = function() {
return this.data.shift();
};
return ShuffleVec;
}
Insert cell
Insert cell
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