# Why do we use rejection sampling even we know the distribution?

I already read this post and I have the exact same questions. Below I pulled the first question and the answer from the post.

Therefore we still use the distribution of p for the randomly generated values x.

Computing the density, $p$, isn't the same as sampling random variables from the distribution $p$ is the density for.

My question is: why computing the density, p, isn't the same as sampling random variables from the distribution p is the density for? In my sense, if I know the pdf and all parameters of a distribution, I can randomly generate a set of x value and I can compute the probability density for each x, then we generate a random variable with a set of x values and probability density, so why do I still need rejection sampling or any other sampling methods to generate a bunch of x and its density? Maybe I misunderstand the meaning of generating random variables?

• Could you explain or illustrate what you do understand by "generating random variables"? – Scortchi Sep 12 '18 at 13:09
• @Scortchi Edited it – Bratt Swan Sep 12 '18 at 13:15
• Still a bit puzzling. When you say "I can randomly generate a set of x values" that's according to what distribution? How would you sample from an exponential distribution with rate parameter 2, say? (Density function $f(x)=2\exp(-2x)$.) – Scortchi Sep 12 '18 at 13:21
• @Scortchi If I have a continuous uniform distribution at [0,2], then its pdf is 0.5. My x values can be manually generated as 0.0001, 0.0002, ..., 1.9999, each value corresponds to probability density 0.5, my random variable X, whose P(X = xi) = 0.5. This is the way how I generate an RV. – Bratt Swan Sep 12 '18 at 13:30
• Even loosely speaking, that's not random number generation. It seems as if your question should have been "why would you ever want to generate a random number?" - there doesn't seem to be any issue specific to rejection sampling. – Scortchi Sep 12 '18 at 14:16

Even if you have the formula for a density, say $p(x)$, in order to generate samples from it, you'll have to devise a method. Many known densities (like exponential, normal, gamma etc.) have library methods to sample from, though they use specific methods to generate those samples. Inverse Transform Sampling is one of the basic of those, which uses uniform RVs. Box-Muller Transform is another, a little bit more complicated (compared to ITS above) method to sample normal RVs. So, how you generate your samples from uniform RVs or any other way is actually another problem; and having the exact $p(x)$ is not that constructive in this way.