Creates a random number generator based on a given probability density function (PDF).
This function discretizes the provided PDF to compute the corresponding cumulative distribution
function (CDF), and returns a function that generates random numbers following the given PDF.
Example
// Define a probability density function constpdf = (x: number) =>Math.exp(-x * x * 4); // Gaussian-like function
// Create a random number generator constrandomGenerator = createRandomGenerator(pdf);
// Generate random samples console.log(randomGenerator());
Parameters
f: (x:number)=>number
The probability density function (PDF). Must be non-negative and normalized over the range [0, 1].
Returns ()=>number
A function that generates random numbers in the range [0, 1] based on the provided PDF.
Creates a random number generator based on a given probability density function (PDF).
This function discretizes the provided PDF to compute the corresponding cumulative distribution function (CDF), and returns a function that generates random numbers following the given PDF.
Example