Published
Edited
Jul 28, 2018
Insert cell
Insert cell
Insert cell
Insert cell
class MonteCarloIntegral extends DefiniteIntegral
{
constructor(samples)
{
super(); // JavaScript nonsense
this.samples = samples;
}
compute(f, a, b)
{
// initialize the sum to 0
let sum = 0.0;
// this is a lambda that will generate a random sample on the interval [a,b)
let sampler = () => a + Math.random() * (b-a);
for(let i = 0; i < this.samples; i++) sum += f(sampler())
return sum * this.delta(a, b, this.samples);;
}
}
Insert cell
Insert cell
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