FIRST, all pre-attack effects happen. For purposes of attack strength, the important parts are:
- Town deaths from any factors other than the zombie attack-- infection, dehydration, being lost in the desert. THIS MAY DECREASE TOWN DEFENSE - dead people's houses/bonuses do not count.
- Votes for town improvements are processed, so if you've voted for the Great Pit or Upgradeable wall, that goes through.
Following that, the attack is calculated. The number of zombies that get through is the Zombie Attack Strength minus the Total Town Defense after deaths/upgrades.
The zombies appear to be assigned randomly amongst all surviving citizens. The number of zombies attacking any given citizen's house is a binomial distribution.
The chances of survival for any given citizen can be calculated Google spreadsheets or most other spreadsheet apps with:
=binomdist(D, Z, P, 1)
D = House Defense Value
Z = Zombies In Town
P = Individual probability, one divided by the number of survivors alive in town.
For example, in a town with 20 survivors, with 38 zombies making it over the wall and a house defense of 3, your odds of survival are:
binomdist(3,38,0.05,1) = 0.8796, or about an 88% chance of living through the attack.
I'm cleaning up the Google Docs spreadsheet that I use to calculate for myself and will put a public link to it here when I'm done.