# Chi Square test of Independence - Why Chi sq distribution?

I read the theory of Chi square test of independence and I am comfortable with the idea of expected counts and the reasoning behind calculating them the way they are. But what bothers me is the selection of Chi square distribution for getting the p-value. Why do we use the Chi square distribution as the distribution to measure against? And how does it work for categorical variables?

The counts random variables can be thought as a sum of independent and identically distributed Bernoulli random variables. Take an example, suppose you have a categorical observations which tells you gender of a person. Thus from a sample on $n$ observations, some of them will be male and some of them not. If all observations are independent identically distributed then you can consider the random variable Male to be a binomial variable, since a binomial is defined as a sum of i.i.d. Bernoulli variables. So we established that a count has a Binomial distribution with $p$ being the probability from the Bernoulli variable and $n$ the number of observations.