Search code examples
rrandomgaussiannormal-distribution

Multiple random values between specific ranges in R?


I want to pick up 50 samples from (TRUNCATED) Normal Distribution (Gaussian) in a range 15-85 with mean=35, and sd=30. For reproducibility:

num = 50 # number of samples
rng = c(15, 85) # the range to pick the samples from
mu = 35 # mean
std = 30 # standard deviation

The following code gives 50 samples:

rnorm(n = num, mean = mu, sd = std)

However, I want these numbers to be strictly between the range 15-85. How can I achieve this?

UPDATE: Some people made great points in the comment section that this problem can not be solved as this will no longer be Gaussian Distribution. I added the word TRUNCATED to the original post so it makes more sense (Truncated Normal Distribution).


Solution

  • As Limey said in the comments, by imposing a bounded region the distribution is no longer normal. There are several ways to achieve this.

    library("MCMCglmm")
    rtnorm(n = 50, mean = mu, sd = std, lower = 15, upper = 85)
    

    is one method. If you want a more manual approach you could simulate using uniform distribution within the range and apply the normal distribution function

    bounds <- c(pnorm(15, mu, std), pnorm(50, mu, std))
    samples <- qnorm(runif(50, bounds[1], bounds[2]), mu, std)
    

    The idea is very basic: Simulate the quantiles of the outcome, and then estimate the value of the specific quantive given the distribution. The value of this approach rather than the approach linked by GKi is that it ensures a "normal-ish" distribution, where simulating and bounding the resulting vector will cause the bounds to have additional mass compared to the normal distribution.

    Note the outcome is not normal, as it is bounded.