Random numbers following a distribution with an interval in R

source https://stats.stackexchange.com/questions/113230/generate-random-numbers-following-a-distribution-within-an-interval

rtruncnorm <- function(N, mean = 0, sd = 1, a = -Inf, b = Inf) {
  if (a > b) stop('Error: Truncation range is empty');
  U <- runif(N, pnorm(a, mean, sd), pnorm(b, mean, sd));
  qnorm(U, mean, sd); }

hist(rtruncnorm(10000, 100, 10, 80, 120))

library(msm)
hist(rtnorm(10000, 100, 10, lower=80, upper=120))