Probability with getting 2s on multiple 2-sided dice vs random number

42 Views Asked by At

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)?

1

There are 1 best solutions below

1
On

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\}$:

  1. Generate a random integer $x$ from $0$ to $2^n - 1$ (inclusive)
  2. Count the number of $1$s in the binary expansion of $x$ (this is often called 'popcount', so look up that term for an efficient way to do it in whatever language/environment you are working in).

This will give you the same distribution as flipping a coin $n$ times and counting the number of heads.