# Generating random samples from a posterior distribution

Let $$p(D \mid \mu,\sigma^2) \sim \mathcal{N}(\mu,\sigma^2)$$ where $$D=(x_1\ldots x_n)$$ is my data. I imposed a normal prior on the mean as $$\pi(\mu) \sim \mathcal{N}(\mu_0,\sigma_0^2)$$ Using Bayes, I know that the posterior is $$p(\mu \mid D) \propto \mathcal{N}(\mu_n,\sigma_n^2)$$ where $$\mu_n$$ is a convex combination between the ML estimate of $$\mu$$ and the initial belief $$\mu_0$$, that is $$\mu_n = w\mu_{ML} + (1-w)\mu_0 \tag{1}$$ $$\sigma_n^{-2} = \frac{n}{\sigma^2} + \frac{1}{\sigma_0^2}\tag{2}$$ Using any random normal generator (R,MATLAB for ex), I could generate $$D$$ using $$\mu,\sigma^2$$.

Question: How on earth do I generate $$\mu \mid D$$ ? I just want to compare the empirical posterior moments, $$\hat{\mu}_n,\hat{\sigma}_n^2$$, to the true ones in equations $$(1,2)$$.

Example: In this question, there is a posterior histogram (evaluated empirically). How do you do that ?