For example with:
Total population = 400000
Number of villages = 800
The average population is 500
How can I randomize the amount of people in each village using a specified deviation (eg +-50) so that it equals the total population?
I came up with a solution using this normal distribution class
Basically I created a normal distribution and used the Probability Density Function to determine the amount of villages at each discrete level (until the population total was hit).
This is something I wrote quickly to demonstrate:
NormalDist dist = new NormalDist(500.0, 50.0);
int popX = 500;
int numberOfPopXVillages = Convert.ToInt32(Math.Floor(dist.PDF(popX) * 800)); ;
int totalSum = 0;
do
{
Console.WriteLine(numberOfPopXVillages);
totalSum += popX * numberOfPopXVillages;
totalSum += (1000 - popX) * numberOfPopXVillages;
++popX;
numberOfPopXVillages = Convert.ToInt32(Math.Floor(dist.PDF(popX) * 800));
} while (numberOfPopXVillages >= 5);
Console.WriteLine(totalSum);
The total sum here is 400000. The algorithm isn't perfect but I'll keep working on it. Thanks everyone.