[Math] How to put probability density function in C++

functionsprobability

I have a random variable X that has a probability density function of f(x)=x^(-1/2)/2 for all x between 0 and 1.

double RANDOM;

I need to give a value to RANDOM, that accepts the PDF

All that I know is that I can use transformation method, where

dx/dy = f(y) so x = sqrt(x), and from that y = x^2

but i don't really get, how it helps me and how can i get, that variable RANDOM is distributed like the PDF.

Thanks!

Best Answer

The usual way to generate values with a distribution identical to that of a random variable $X$ is to observe that if $U\sim \operatorname{Unif}(0,1)$ then $F_X^{-1}(U)\sim X$ (here $F_X$ is the cumulative distribution function of $X$).

So if you can come up with a function representing your CDF, just apply its inverse to your usual random numbers in $(0,1)$.