Published
Edited
Oct 17, 2019
1 fork
Insert cell
md`# Sorteando com probabilidades dadas`
Insert cell
prop = [1,1,10]
Insert cell
{
let s = prop.reduce((a,b)=> a+b)
let t = prop.map((x,i) => prop.slice(0,i+1).reduce((a,b)=>a+b) /s)
return t;
}
Insert cell
sample = {
let s = prop.reduce((a,b)=>a+b);
let n = prop.length;
let threshold = prop.map((x,i) => prop.slice(0,i+1).reduce((a,b)=>a+b) /s);
return () => {
let x = Math.random();
for (let i = 0; i < threshold.length; i++) {
let t = threshold[i];
if (x <= t) return i;
}
}
}
Insert cell
{
let r = [];
for (let i = 0; i < 100; i++) r.push(sample())
return r;
}
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