I need to roll n 2-sided dice (also known as coin tosses), and count the number of 2s (or heads) in the result.
Since I need lots of those in a computer program I figured I could maybe optimize that by instead just taking a random number between 0 and n, but i'm not sure if that's the same probability distribution?
I know the distribution of the total result of n d2 is a bell curve, but I don't know this about 2s in n d2, and for my optimization to work it would have to be evenly distributed (which I instinctively feel it should be, since all dice have the same probability of landing on either side, but I might be wrong here, been a while since I theoretically learned this stuff back in school, and forgot a lot).
So my question is: is "take the number of 2s in n d2" the same as "take a random integer between 0 and n", and if not, why, and is there any other way i could "streamline" that calculation (because computers notoriously suck at drawing lots of random numbers fast)?
Since you said that $n$ is small, here is a way to do it that will almost certainly be quicker than taking $n$ samples from the uniform distribution on $\{0, 1\}$:
This will give you the same distribution as flipping a coin $n$ times and counting the number of heads.