Published
Edited
Feb 13, 2022
Insert cell
Insert cell
Insert cell
class Walker {
constructor(width, height) {
this.x = width / 2;
this.y = height / 2;
}

display(context) {
context.fillStyle = "crimson";
context.fillRect(this.x, this.y, 3, 3);
}

step() {
let stepX, stepY;
let r = Math.random();
//const random = () => Math.round(montecarlo()*2 - 1);
const random = r < 0.01 ? d3.randomInt(-50, 51) : d3.randomInt(-1, 2);
this.x += random();
this.y += random();
}
}
Insert cell
function montecarlo() {
while (true) {
const r1 = Math.random();
const probability = r1;
const r2 = Math.random();
if (r2 < probability) {
return r1;
}
}
}
Insert cell
walker = new Walker(width, height);
Insert cell
width = 320
Insert cell
height = 256
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