How can I simulate using Inverse and Rejection method for $f(x)=\left\{\begin{matrix} \frac{1}{2} & 0\leq x\leq 1 \\ \frac{1}{4} & 2\leq x\leq 4 \\ 0 & otherwise \end{matrix}\right.$
I found the cdf $F(x)=\left\{\begin{matrix} 0 & x < 0 \\\frac{x}{2} & 0\leq x< 1 \\ \frac{x}{4} & 2\leq x< 4 \\ 1 & x\geq 4 \end{matrix}\right.$ and the inverse $F^{-1}(x)=\left\{\begin{matrix} 2x & 0\leq x< 1 \\4x & 2\leq x< 4 \end{matrix}\right.$
but I don't know how to apply the algorithms for a function defined in this way, I didn't find any examples.
For the inverse method I generate $U\sim Unif(0,1)$ and take $F^{-1}(U)=X$, but how do I do this? Do I use just the $0\leq x< 1$ interval because $U$ takes values just there?
For the rejection method I took
$X_{1}\sim Unif(0,1)$ $f_{1} = \frac{1}{2}$ and $Y_{1} \sim Unif(0,1)$ $g_{1} = 1$ , for both $f_{1}$ and $g_{1}$ $0\leq x\leq 1$
$X_{2}\sim Unif(0,1)$ $f_{2} = \frac{1}{4}$ and $Y_{2} \sim Unif(2,4)$ $g_{2} = \frac{1}{2}$ , for both $f_{2}$ and $g_{2}$ $2\leq x\leq 4$
and for both $c= \frac{1}{2}$.
If I had the function just for $0\leq x\leq 1$ I would generate $U_{1},Y_{1} \sim Unif(0,1)$ and $X=Y_{1}$ if $U_{1} \leq \frac{f_{1}}{c \cdot g_{1}}$, but how do I do this for my function?