STAT2001 Revision Summaries
  • Home
  • About This Site
  • STAT2001 Weekly Topics
Simulation
  • Plot
seedrandom = require("seedrandom@3.0.5")
d3 = require("d3@7")
import { Plot } from "@observablehq/plot"
rng = seedrandom(2025);  // ✅ do this in a normal ojs block
randomValues = rng();  // true cutoff

// Define theta as a reactive variable that updates when the button is clicked
theta = {
  randomizeTheta; // Ensures reactivity
  return rng();
}
chalks = {
  regenerateChalks; // Ensures reactivity
  return Array.from({ length: 5 }, () => rng());
}

result = chalks.map(x => ({
  x,
  color: x < theta ? "steelblue" : "lightgray"
}));

Plot.plot({
  width: 600,
  height: 80,
  marks: [
    Plot.ruleX([theta], {stroke: "red", strokeDasharray: "4,2", strokewidth: 2}),
    Plot.dotX(result, {
      x: d => d.x,
      r: 7,
      fill: d => d.color,
      stroke: "black"
    })
  ],
  x: {
    domain: [0, 1],
    label: "table position"
  },
  y: {axis: null},
  margintop: 30,
  marginbottom: 30
})
viewof seed = Inputs.number({label: "Random Seed", value: 2025, step: 1})
viewof randomizeTheta = Inputs.button("Randomize θ")
viewof regenerateChalks = Inputs.button("Regenerate Chalks")

Copyright 2025, Isaac Leong

 
  • Edit this page
  • View source
  • Report an issue