# Generate Random number for U shaped distribution

Hi everyone

I wanna ask for generating random number in U shaped distribution which limit +a and limit -a are the edges

Thanksss for helping me

``````# inspiration >>
#           https://stats.stackexchange.com/a/448477

dparabola <- function(x,left,right,center_height_shift){
form <-  function(x0){center_height_shift + (x0-mean(c(left,
right)))^2}
left_lim <- form(left)
step_1 <- ifelse(x < left | x > right, 0, form(x))
step_1/ left_lim # scale to between 0 and 1 based on the height of the left limit
}

myparab0 <- purrr::partial(dparabola,left=2,right=6,center_height_shift=0)
curve(myparab0, from=-1, to=7, col="red")

myparab10 <- purrr::partial(dparabola,left=2,right=6,center_height_shift=10)
curve(myparab10, from=-1, to=7, col="blue" , add=TRUE)

# get a random y from myparab10 the blue curve
(rand_x <- runif(n=1,min=2,max=6))
myparab10(rand_x)``````

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.