Random draw from a distribution


#1

Hi, I wonder if anybody can explain why the following two codes do not generate the same output although it's supposed to.

I have written a simple code to draw observations from a distribution as follows

for (n in 1:nsim){
out[n] <-Position(function(x) x>runif(1),cdf)
}

This code does not draw from cdf distribution correctly.
However, when I used runif(1) outside like this

for (n in 1:nsim){
rr[n] <- runif(1)
out[n] <-Position(function(x) x>rr[n],cdf)
}

things worked correctly.
It looks super odd to me why these two make such a huge difference.
Can anybody have any guess on this weird behavior?

Thanks !!