Function createRandomGenerator

  • 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.

    // Define a probability density function
    const pdf = (x: number) => Math.exp(-x * x * 4); // Gaussian-like function

    // Create a random number generator
    const randomGenerator = createRandomGenerator(pdf);

    // Generate random samples
    console.log(randomGenerator());
    • randomFunctions

    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.