Published
Edited
Nov 2, 2021
1 star
Insert cell
Insert cell
a = [1,7,3]
Insert cell
makeWheel = (a) => {
const sum = d3.sum(a)
return a.map(d => {
return {value:d, prob:d/sum}
})
}
Insert cell
wheel = makeWheel(a)
Insert cell
spinWheel = (wheel) => {
const probs = wheel.map(slice => slice.prob)
const cprobs = d3.cumsum(probs)
const r = d3.randomUniform(0,1)()
return [...cprobs,r].sort((a,b) => a - b).indexOf(r)
}
Insert cell
results = {
const results = []
for(let i = 0;i < 10000; i = i + 1){
const index = spinWheel(wheel)
results.push(wheel[index].value)
}
return results
}
Insert cell
d3.bin()(results).map(a => a.length/10000)
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