\(F(x) = 0, x <= -3\)
\(F(x) = \frac{1}{2} + \frac{x}{6}, -3 < x <= 0\)
\(F(x) = \frac{1}{2} + \frac{x^2}{32}, 0 < x <= 4\)
\(F(x) = 1, x > 4\)
generator <- function(x){
fx <- 0
if (x > -3 & x <= 0) { fx <- 1/2 + x/6}
if (x > 0 & x <= 4) { fx <- 1/2 + x**2/32}
if (x > 4) {fx <- 1}
return(fx)
}
# set the parameters
i <- 1000
min <- -3
max <- 4
# get 1000 random numbers
rm <- runif(i,min, max)
# feed through cdf function
rv <- sapply(rm, function(x) generator(x))
hist(rm, main='Histogram of 100 Random Numbers Generated')
hist(rv, main='Histogram of 1000 Values Using CDF function')
The problem was not as challenging but it was interesting to see how the histogram shows the distribution of generated values. Most values fall between 0.5 and 0.6.