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.

The question doesn't require a specific programming language, which is fine, but I noted that the OP's plot looks like the default style of matplotlib. @jbowman has given a useful r implementation. Here is a similar python implementation in case the OP would like to continue their project in python.
First we can define a function rsin.
import numpy as np

def rsin(n):
    results = np.zeros(n)
    for i in range(n):
        while True:
            u = np.random.uniform(0, 2*np.pi)
            u2 = np.random.uniform()
            if u2 <= np.abs(np.sin(u)):
                results[i] = u
                break
    return results

And then we can plot the results using matplotlib.
import matplotlib.pyplot as plt

plt.hist(np.cos(rsin(10000)), bins=20)
plt.show()

Which produces the following plot:


@schotti's approach can be given (ignoring some of the style elements) in python as follows.
import matplotlib.pyplot as plt

x = np.arccos(np.random.uniform(size=10**5))
y = np.cos(x)

fig, axes = plt.subplots(1,2)

axes[0].hist(x)
axes[1].hist(y)

for axis in axes:
    axis.set_xlabel('x')
    axis.set_ylabel('Frequency')
    axis.tick_params('y', labelrotation=90)

plt.show()


