Problems using Rejection Sampling method I'm supposed to generate random numbers from the following distribution:
$$ f(x) = \begin{cases} \frac{3}{4}(2x-x^2) &\mbox{if } x \in (0,2) \\ 
0 & \mbox{else} \end{cases} $$
I'm given the following algorithm in my script, which looks slightly different from those that I have found in the literature:


*

*Simulate $ U \sim U(0,1)$

*Simulate $Y \sim q$

*Accept $X=Y$ if $ U \leq \dfrac{1}{M}\dfrac{f(Y)}{q(Y)}$, otherwise go to step 1.


Now first I have to find a function q which is easier to sample from, such that there exists a $M \in \mathbb{R}$, so that $Mq(x) \geq f(x), \forall x \in (0,2)$.
I decided to pick $q \sim U(0,2)$ and have $M := \sup_{x \in (0,2)} f(x) = \frac{3}{4}$
Now I sample from $U(0,1)$, for which I get $U = 0.32$, then I sample from $Y \sim q \Rightarrow 1.28$ and now I'm supposed to accept the sampled value $y$ from step 2 if $ U \leq \frac{1}{M} \frac{f(Y)}{q(Y)}$ which in my case gives me: $0.32 \leq \frac{4}{3}f(1.29)=0.92$, so I'm supposed to accept $X=1.28$, however $1.28$ can hardly be from $f$. So what am I doing wrong.
 A: I think part of your original confusion is that your 'envelope' function is
too simple. Let's start (as in the crucial comment by @Ian)
by generating points uniformly in the rectangle that encloses your PDF
$f(x) = 0.75(2x - x^2),$ for $x \in [0,2].$ Points (blue) that fall under
the PDF curve are accepted and those above it (orange) are rejected.
A histogram of the accepted x-values fits the PDF well. You can
verify by integration that the simulated values of $E(X)$ and $SD(X)$
are correct within simulation error. (My R code below, saves all
points and then settles which ones are accepted at the end.)
 B = 40000;  M = 3/4
 x = runif(B, 0, 2);  y = runif(B, 0, M)
 acc = y <= M*(2*x - x^2)
 mean(x[acc]);  sd(x[acc])
 ## 1.002849  # Simulated E(X)
 ## 0.446792  # Simulated SD(X)

 par(mfrow = c(1,2))  # side-by-side plots
   plot(x, y, pch=".", col="red")
     points(x[acc], y[acc], pch=".")
   hist(x[acc], prob=T, col="wheat")
     curve(.75*(2*x - x^2), 0, 2, lwd=2, col="blue", add=T)
 par(mfrow = c(1,1))  # restore default plotting


In practice, it often 'wastes' too many candidate values to
simulate within a rectangle, so an 'envelope' function is chosen
for the upper boundary.
In your more general notation, the envelope function is $M$ times the 
density function of $Unif(0, 2)$. It may help you to remember how
this method works if you rewrite my code in your more general
notation.
Notes: (a) Your PDF is the density function of $X$ where $X = 2U$ and $U \sim Beta(2,2).$ In R, you could simulate this distribution
using 2*rbeta(B, 2, 2) where the random sampling function rbeta is built into R. 
 w = rbeta(100000, 2, 2)
 mean(2*w);  sd(2*w)
 ## 1.001450  # Compare with simulated mean and SD above
 ## 0.4466403

(b) If you wanted to sample from $Beta(3,1)$
using the rejection method, there is a natural nontrivial envelope: you could use the envelope function $3x$ which
can be considered a multiple of the triangular density function
of $Beta(2,1).$ (It is easy to simulate observations from $Beta(2,1)$ using the inverse CDF method and the envelope is 'close enough'
that only a small proportion of candidate values will be rejected.)

