monte_carlo
Builtin index · Signal processing
result = monte_carlo(f, n, [seed=])
Input: f, n, seed · Output: result
What it is
f: a string naming a zero-argument user-defined function (typically drawing its own randomness via randn/rand) returning a number; n: a number (integer), how many times to call it; seed (optional number).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = monte_carlo(f, n, [seed=]) | f string or numberf: a string naming a zero-argument user-defined function (typically drawing its own randomness via randn/rand) returning a number.n numbern: a number (integer), how many times to call it.seed number, optionalseed (optional number). | result vector | Returns a length-n vector collecting every result. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
sample() := randn()
draws = monte_carlo("sample", 10000, seed=1)
print("mean = {mean(draws)}, std = {std(draws)}")
mean = 0.001862, std = 1.008776
from numerics-dsp
How it works
monte_carlo(f, n, [seed=]) — repeatedly calls the zero-argument user function f (typically drawing its own randomness via randn/rand) n times and collects every result into a vector — the general pattern most Monte Carlo simulations need, without prescribing what f computes.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.