What distribution to sample X from to get an uniform distribution in Y? I have a random variable $X$ which is related to another random variable $Y$ as $Y = \text{cos}(X)$, where $X \in [0, \pi/2]$, and I would like to know what distribution I should sample $X$ from in order that $Y$ has a uniform distribution. A simple Jacobian transformation says that
$f(Y) = |\frac{\partial X}{\partial Y}| f(X)$
which suggest that in order to have $f(Y) = 1$ (i.e. uniform), we should equate $f(X) = |\frac{\partial Y}{\partial X}| $. In this specific example that would mean we need to sample $x$ from the distribution $f(X) = |\text{sin(X)}|$.
However when I do this in practice i.e. I plug a sample of $x$ values drawn from a sine distribution into the cosine, the resulting distribution of $y$ values is not uniform at all, but rather looks like this.

Could someone please point out what I am doing wrong?
Thanks in advance.

There is no need for absolute values around your sine function. f(x) = sin(x) is a perfectly fine pdf on the sample space [0, pi/2]. As schotti points out, you can create an RV with this pdf by taking the inverse cosine (arccos) of a standard uniform RV; this follows from the Jacobian transformation that you have given in your question. That being the case, undoing the arccos by applying the cosine will obviously take you back to the standard uniform that you began with. Don't believe this? Here is an image of a pdf histogram (total area = 1) for a sample of 10^5 standard uniforms transformed by arccos, with the graph of sine superimposed. Clearly, this works.

It's important to use a standard uniform, with sample space [0,1] as input of the arccos and the output of the cosine. Obviously, if the upper endpoint is greater than 1, you will get errors from the arccos, and impossible values for the cosine transformation. The real danger is negative values. If your uniform variable input generates negative values, arccos will take you to values above pi/2, and you cease to have sine as a valid pdf. You could work with a uniform RV with sample space [-1,1], but the RV resulting from the arccos will have sin(x)/2 as its pdf, not the sin(x) that you desire. Here is that situation:

