# How do you sample $\mathcal N(\mu, \sigma^2)$ from a range?

How can we generate a sample in the interval $[a,b]$ based on a Gaussian distribution?

If we have a Gaussian random generator, just by mapping the number to the range and pruning (ignoring) the values outside of the range, does our sample still follow the Gaussian distribution? In short, how correct is this approach?

added later : assume we can generate $\mathcal N(\mu, \sigma^2)$ with any parameters, now with given [a,b] , is there a way to chose a relatively good $\mathcal (\mu)$ and $\mathcal (\sigma)$ ? for example $\mathcal \mu = (a+b)/2$ & $\mathcal \sigma = (b - \mu)/3$

• Do you mean sampling from truncated normal? – Tim Oct 6 '16 at 14:54
• That would only happen if the truncation is symmetric about the mean ($a=\mu-c$, $b=\mu+c$); otherwise the mean will be elsewhere. When you specify $\sigma$ are you specifying the parameter of the original untruncated normal, or the standard deviation of the truncated one? – Glen_b Oct 6 '16 at 15:07
• There are a number of questions on site that deal with simulation of truncated normals. See this post, for example stats.stackexchange.com/questions/142999/… (and the linked paper there -- I realize you don't have a mixture, but that part is straightforward -- most of the discussion focuses on the truncation). Or see this post – Glen_b Oct 6 '16 at 15:13
• Your drawing does NOT have mu = (a+b)/2. Your attempted clarification has introduced new problems so I have had to close again as unclear. Note that a truncated normal is not normal, so the answer to "does our sampling still follow the Gaussian distribution" is no -- it follows a truncated Gaussian. – Glen_b Oct 6 '16 at 15:32
• No, because then they wouldn't be truncated. If you mean can you generate random normals then throw away any outside the boundaries to get a truncated normal, I believe this is already answered in the paper that Xi'an links to there. It's not made much of, though so perhaps you missed it. – Glen_b Oct 6 '16 at 15:48

While several posts on site link to things that at least mention your proposed algorithm, and some posts seem to mention it in passing, I didn't locate a post that directly discussed it in enough detail to really count as answered on this site. As a result I think it's worth discussing briefly here, but you should refer to the posts I linked in comments -- Simulate from a truncated mixture normal distribution (and the linked paper there), and Simulate constrained normal on lower or upper bound in R -- for good algorithms.

1. You can get truncated normals in the way you suggest - by generating normals and throwing out values outside the truncation bounds - so it is correct but it's usually not used (except as part of a series of possible methods) because it can be very inefficient.

Consider for example if you have $$\mu=0$$, $$\sigma=1$$ but $$a=4$$ and $$b=5$$. Your method would generate over thirty-thousand normal values for every one it kept. It works fairly well when both $$a<\mu-\sigma$$ and $$b>\mu+\sigma$$ (though it can be used when they aren't both outside that range if need be).

2. Another method that can be useful when $$a$$ and $$b$$ are both close to $$\mu$$ (say both between $$\mu-\sigma$$ and $$\mu+\sigma$$) is to sample a random point in the rectangle with base (a,b) and height the maximum height of the normal density in the interval, then return the ordinate of the point if the point falls under the normal density (otherwise reject it and generate again). This is a different form of accept-reject to that in 1., and useful in different circumstances. It's fairly easy to compute the rejection rates of the two approaches in 1 and 2, so if the cost of generating two uniforms isn't much different from the cost of generating one normal you'd pick the one with the lower rejection rate.

3. You can also use the inverse cdf method. This can work quite well.

4. In the extreme tails you can use accept-reject with an exponential majorizing function (an approach I first read about something like 30 years ago, but I can't locate the reference at the moment).

More details of these other options are available in the resources previously mentioned.

• Thank you Glen, I'm coding in abap and I don't have "r" or "java" functionalities, I can generate random number with desired mu and sigma, So thanks to your guide I'm going to use it when a<μ−σ and b>μ+σ – khodayar J Oct 6 '16 at 16:33
• The methods being discussed don't require R or java. But in any case method 2 needs nothing more than a uniform RNG and the ability to do simple calculations -- and it should work in many cases method 1 doesn't do so well. Method 4 is very easy to implement if you have a uniform RNG and the ability to take logs. Between them, 1, 2 and 4 should cover most situations quite well. – Glen_b Oct 6 '16 at 17:04
• @Xi'an , yes, I mean gaussian random numbers – khodayar J Oct 7 '16 at 16:51
• @Xi'an Yes, I understand that you do. I made that explicit in another answer a couple of days ago -- here. I feel that you're asking me to correct a mistake here or perhaps mention you explicitly here as well but it's not quite clear to me which it is. Did I say something wrong? – Glen_b Oct 7 '16 at 23:20
• @Xi'an The reference I meant above was a much earlier reference to the technique; I saw it some time in the 80s. I think it was in relation to one of the many methods for generating normal random variates where it did the tail separately, and that was one of the suggestions for the tail. I used it myself to generate observations from the normal tail in the late 80s or thereabouts – Glen_b Oct 8 '16 at 4:56