[deutsch]

Generating pseudo-random numbers

Kontrast provides a function that generates pseudo-random numbers drawn according to a specified probability distribution.

There are two available distributions: uniform and gaussian that are specified with the property distribution. The number of random numbers to be generated is specified using the property count.

The uniform distribution requires to specify an inclusive lower limit (l, property lowerLimit) and an exclusive upper limit (u, property upperLimit). The probability distribution is:

p(x) = \begin{cases}(u - l)^{-1}\quad&\text{if}\quad l \le x \lt u\\0&\text{otherwise}\end{cases}

The Gaussian distribution requires to specify the mean (\mu, property mean) and the standard deviation (\sigma, property standardDeviation). The probability distribution is:

p(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp{\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)}

Kontrast relies on the function window.crypto.getRandomValues, which is designed to generate better pseudo-random numbers compared to Math.random.

Interactive code example

Input

The function is called as kontrast.generateRandomNumbers(options), where options is an object with the following properties:
  • distribution: 'uniform' | 'gaussian'
    The distribution according to which the random numbers are drawn.
  • count: number
    The length of the output array, i.e., the number of random numbers to be generated.
  • lowerLimit: number
    The lower limit for the random numbers (only applies to the uniform distribution).
  • upperLimit: number
    The lower limit for the random numbers (only applies to the uniform distribution).
  • mean: number
    The average of the distribution (only applies to the Gaussian distribution).
  • standardDeviation: number
    The standard deviation of the distribution (only applies to the Gaussian distribution).

Output