Sample from a Normal distribution but ignore all the random values
falling outside the specified range before simulations.
This method is correct, but, as mentionned by @Xi'an in his answer, it would take a long time when the range is small (more precisely, when its measure is small under the normal distribution).
As any other distribution, one could use the inversion method $F^{-1}(U)$ (also called inverse transform sampling), where $F$ is the (cumulative function of the) distribution of interest and $U\sim\text{Unif}(0,1)$. When $F$ is the distribution obtained by truncating some distribution $G$ on some interval $(a,b)$, this is equivalent to sample $G^{-1}(U)$ with $U\sim\text{Unif}\bigl(G(a),G(b)\bigr)$.
However, and this is already mentionned by @Xi'an's in a comment, for some situations the inversion method requires a very precise evaluation of the quantile function $G^{-1}$, and I would add it also requires a fast computation of $G^{-1}$. When $G$ is a normal distribution, the evaluation of $G^{-1}$ is rather slow, and it is not highly precise for values of $a$ and $b$ outside the "range" of $G$.
Simulate a truncated distribution using importance sampling
A possibility is to use importance sampling. Consider the case of the standard Gaussian distribution ${\cal N}(0,1)$. Forget the previous notations, now let $G$ be the Cauchy distribution. The two above mentionned requirements are fulfilled for $G$ : one simply has $\boxed{G(q)=\frac{\arctan(q)}{\pi}+\frac12}$ and $\boxed{G^{-1}(q)=\tan\bigl(\pi(q-\frac12)\bigr)}$. Therefore, the truncated Cauchy distribution is easy to sample by the inversion method and it is a good choice of the instrumental variable for importance sampling of the truncated normal distribution.
After a bit of simplifications, sampling $U\sim\text{Unif}\bigl(G(a),G(b)\bigr)$ and taking $G^{-1}(U)$ is equivalent to take $\tan(U')$ with $U'\sim\text{Unif}\bigl(\arctan(a),\arctan(b)\bigr)$:
a <- 1
b <- 5
nsims <- 10^5
sims <- tan(runif(nsims, atan(a), atan(b)))
Now one has to calculate the weight for each sampled value $x_i$, defined as the ratio $\phi(x)/g(x)$ of the two densities up to normalization, hence we can take
$$
w(x) = \exp(-x^2/2)(1+x^2),
$$
but it could be safer to take the log-weights:
log_w <- -sims^2/2 + log1p(sims^2)
w <- exp(log_w) # unnormalized weights
w <- w/sum(w)
The weighted sample $(x_i,w(x_i))$ allows to estimate the measure of every interval $[u,v]$ under the target distribution, by summing the weights of each sampled value falling inside the interval:
u <- 2; v<- 4
sum(w[sims>u & sims<v])
## [1] 0.1418
This provides an estimate of the target cumulative function.
We can quickly get and plot it with the spatsat
package:
F <- spatstat::ewcdf(sims,w)
# estimated F:
curve(F(x), from=a-0.1, to=b+0.1)
# true F:
curve((pnorm(x)-pnorm(a))/(pnorm(b)-pnorm(a)), add=TRUE, col="red")

# approximate probability of u<x<v:
F(v)-F(u)
## [1] 0.1418
Of course, the sample $(x_i)$ is definitely not a sample of the target distribution, but of the instrumental Cauchy distribution, and one gets a sample of the target distribution by performing weighted resampling, for instance using the multinomial sampling:
msample <- rmultinom(1, nsims, w)[,1]
resims <- rep(sims, times=msample)
hist(resims)

mean(resims>u & resims<v)
## [1] 0.1446
Another method: fast inverse transform sampling
Olver and Townsend developed a sampling method for a broad class of continuous distribution. It is implemented in the chebfun2 library for Matlab as well as the ApproxFun library for Julia. I have recently discovered this library and it sounds very promising (not only for random sampling). Basically this is the inversion method but using powerful approximations of the cdf and the inverse cdf. The input is the target density function up to normalization.
The sample is simply generated by the following code:
using ApproxFun
f = Fun(x -> exp(-x.^2./2), [1,5]);
nsims = 10^5;
x = sample(f,nsims);
As checked below, it yields an estimated measure of the interval $[2,4]$ close to the one previously obtained by importance sampling:
sum((x.>2) & (x.<4))/nsims
## 0.14191