Published
Edited
Feb 4, 2022
Insert cell
Insert cell
Insert cell
function d(n) {
return Math.trunc(Math.random() * n + 1)
}
Insert cell
Insert cell
Insert cell
Insert cell
Plot.rectY(samples, Plot.binX({y:'count'}, {x:'roll', thresholds: 100, fill: 'steelblue'}))
.plot()
Insert cell
Insert cell
function oe_low() {
let result = d(100), next = 0
if (result > 5) return result
do {
next = d(100)
result -= next
} while (next > 95)
return result
}
Insert cell
Insert cell
function oe_high() {
let result = d(100), next = 0
if (result <= 95) return result
do {
next = d(100)
result += next
} while (next > 95)
return result
}
Insert cell
Insert cell
function oe100() {
let result = d(100), next = 0
if (result > 95) {
do {
next = d(100)
result += next
} while (next > 95)
}
if (result < 5) {
do {
next = d(100)
result -= next
} while (next > 95)
}
return result
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.rectY(oe_low_samples, Plot.binX({y:'count'}, {x:'roll', thresholds: 300}))
.plot()
Insert cell
Insert cell
Plot.rectY(oe_high_samples, Plot.binX({y:'count'}, {x:'roll', thresholds: 300}))
.plot()
Insert cell
Finally, for the Open-ended d100 case
Insert cell
Plot.rectY(oe_samples, Plot.binX({y:'count'}, {x:'roll', thresholds: 500}))
.plot()
Insert cell
Insert cell
two_roll_samples = {
let samples = []
for (let roll1 = 1; roll1 <= 100; roll1++) {
for (let roll2 = 1; roll2 <= 100; roll2++) {
let roll = (roll1 < 5) ? roll1 - roll2 : ((roll1 > 95) ? roll1 + roll2 : roll1)
samples.push({roll})
}
}
return samples
}
Insert cell
Insert cell
Plot.rectY(two_roll_samples, Plot.binX({y:'count'}, {x:'roll', thresholds: 300}))
.plot()
Insert cell
Insert cell
Insert cell
Plot.rectY(three_roll_samples, Plot.binX({y:'count'}, {x:'roll', thresholds: 200}))
.plot()
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