This question might appear stupid, but I haven't found how to do it:
I want to simulate a sampling on a large number of samples (very large n, in the distribution $b(n, p)$, and count the results.
The only way I know is to repeat the experiment n times, each time selecting the outcome 1 with probability p, and 0 with probability (1-p), and sum the results at the end.
However, when n is very large, this becomes impractical. Is there an other, faster way to do it ?
Thanks
Sampling from the binomial distribution over a wide range of values of $n$ and $p$ can't really be done in the same way in all cases. Instead we should use the normal or Poisson approximations to handle large $n$, and the exact distribution to handle small $n$.
Assuming $p \leq 1/2$, one can use a normal approximation for $np \gg 1$ and a Poisson approximation for $n \gg 1$ such that $np \sim 1$. (In practice, "$np \gg 1$" means essentially "$np>20$".) For $p>1/2$ one can draw a sample with $p$ replaced by $1-p$ using one of the above methods, and then subtract the result from $n$.
Note that although it might seem that a Poisson is at least as hard to sample as a binomial, you can write the CDF in terms of the incomplete Gamma function to make it cheap to evaluate.