Simulation of the pdf of a random variable I want to simulate the pdf of the random variable given in the answer here: Distribution of $n\choose x$ . How can I simulation the pdf of $\binom{n}{X}$ in Matlab in way to confirm that it's indeed what the answer says?  
 A: You have a binomial random variable $X \sim B(n, p)$, where $n$ and $p$ are given parameters. Now, you want to consider another random variable:
$$Y = \binom{n}{X}$$
$Y$ is a function of $X$ so, to sample from $Y$, first draw samples of $X$. Sampling from a binomial distribution is straightforward. Since you're using Matlab, you can use binornd() with your chosen parameters. Then, pass these samples through the nchoosek() function to obtain samples of $Y$. That is, given samples $\{x_1, \dots, x_n\}$, corresponding samples $\{y_1, \dots, y_n\}$ are given by:
$$y_i = \frac{n!}{x_i! (n-x_i)!}$$
Once you have a large number of samples of $Y$, you can estimate its distribution using the frequency of each value, then compare this to your analytical solution. You can also run goodness-of-fit tests to determine whether the samples match your analytical solution.
Note that  $\binom{n}{k}$ grows very quickly (past the computer's capacity to represent numbers using standard formats), so you won't be able to compute it for very large values without playing some special tricks. Otherwise you can stick to moderate values of $n$.
A: If you toss a coin $n$ times, and it has a $0.5$ probability head(and tail), then the probability that you get $x$ heads is proportional to $\binom{n}{X}$.
This means that you need to call rand() to generate $n$ uniform random variables, and count how many of them are larger than $0.5$ . This would correspond to a random variable with distribution $P(X=x) = {\binom{n}{x}} / 2^n$.
