\[ F(x)=\left\{ \begin{array}{c l} 0, & x \leq -3\\ 1/2 + x/6, & -3 < x \leq 0\\ 1/2 + x^2/32, & 0<x\leq 4 \\ 1, & x >4 \end{array}\right. \]

Since in this example we won’t ever get an x outside the range of -3 < x < 4

\[ F(x)=\left\{ \begin{array}{c l} 1/2 + x/6, & -3 < x \leq 0\\ 1/2 + x^2/32, & 0<x\leq 4 \\ \end{array}\right. \] Inverse transform above CDF

\[ F^{-1}_{X}(U)=X=\left\{ \begin{array}{c l} 6U-3, & 0 \leq U \leq .5\\ 4\sqrt{2U-1}, & .5 < U \leq 1 \\ \end{array}\right. \]

inverse = function(u){
  ifelse(u < .5, 6*u-3,4*sqrt(2*u-1))
}

u = sapply(runif(1000,0,1),inverse)

hist(u,freq = FALSE,main='1000 data point plot')
lines(density(u))

plot(ecdf(u),main='CDF')

Thanks to other discussion posts and the best rated answer on this thread : https://stats.stackexchange.com/questions/184325/how-does-the-inverse-transform-method-work