Search code examples
javarandom

Is there a way to create a random vnumber following some general distribution


I've been stuck with this problem and I don't know what to do. Suppose a general function of distribution f(x). For simplicity let us suppose that integral of f(x) is 1 in the range of our random numbers

How does one create a function that would return a random number, such that distribution of those random numbers over numberline would be f(x)?

My attempt:

Create two random numbers with no distribution between 0 and 1: a and b

If f(a)<b then return a, else go recursive.

The problem is that 0<=b<=1, but f(a) can have any value from 0 up to infinity, so there would points, where point would get for sure


Solution

  • The general problem of sampling from probability distributions is well-studied, and a number of techniques are available based on the specific case. Generally, something about the function must be known: a samplable distribution which has a constant multiple that bounds the target distribution, its integral/CDF, for example. Wikipedia has a list of options.