# How to generate conditioned random variables from a density function?

I want to generate random variables from a distribution function using inverse sampling with the additional condition that the sampling should be conditioned, i.e., random generated variables should be greater or less than a given value.

The inverse of my cdf is :

invcdf <- function(y) a2 * log(a1/y - 1) + a3


a1,a2 and a3 are parameters.

I used inverse sampling to generate n rv as follows :

invcdf(runif(n))


Now, the problem is that I want the values generated greater than a value. How should I introduce this condition in random sampling?

When I use this to have value greater than 500 :

invcdf(runif(10,500,1e6))


I get this error message : Warning message: In log((a0/y) - 1) : NaNs produced This is due to the fact that R is trying to take the logarithm of a negative number for a high value of y (a1=1).

I already try to repeat the process until having values satsifying my constraints is this way :

repeat{
x=invcdf(runif(1))
if(x>500){
break
}


But it tooks a lot of time and sometimes can't provide the value that I need.

repeat{