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;
     totalSum += popX * numberOfPopXVillages;
     totalSum += (1000 - popX) * numberOfPopXVillages;
     numberOfPopXVillages = Convert.ToInt32(Math.Floor(dist.PDF(popX) * 800));
 } while (numberOfPopXVillages >= 5);

The total sum here is 400000. The algorithm isn't perfect but I'll keep working on it. Thanks everyone.

  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Commented Apr 13, 2014 at 9:09
  • If this is intended to be the answer, then please answer your own question. Including the answer in the question confuses everyone.
    – david.pfx
    Commented Apr 13, 2014 at 9:59
  • "Users with less than 10 reputation can't answer their own question for 8 hours after asking. You can answer 4/13/2014 4:25:05 PM. Until then please use comments, or edit your question instead."
    – user126795
    Commented Apr 13, 2014 at 10:04

1 Answer 1


First, you use Normal distribution to generate population of each village. This should give you number that is pretty close to total population. To get exact population, just add or remove the difference evenly across all villages.

The problem of this algorithm is that there is some probability of generating negative population. But that heavily depends on parameters. For parameters from your example, the probability is extremely slim. But for parameters (10000, 100, 50), the probability is there.

import random

def generate_villages(total, count, deviation):
    average = total / count
    villages = [random.gauss(average, deviation) for _ in range(count)]
    diff = (sum(villages) - total)/count
    villages = [round(v - diff) for v in villages]
    return villages

vil = generate_villages(400000, 800, 50)

While this code doesn't give precise number. It deviates +-10 which is fine.

  • @user126795 This answer is probably useful, but not technically correct. E.g. the normal distribution assigns a (negligible) probability to negative values as well, but there is no proper interpretation for a negative population. The normal distribution is also continuous, whereas population is discrete. It would be good to investigate non-negative, discrete distributions like the Poisson distribution (although that specific distribution doesn't allow arbitrary deviations as the standard deviation is always the sqrt of the mean).
    – amon
    Commented Apr 13, 2014 at 9:14
  • @amon What negative population when center of distribution is in 500 with deviation of 50? Most of the populations will fall in range of 450-550 with minimum between 400-600. Also, what stops you from round the real number you get from distribution to an integer?
    – Euphoric
    Commented Apr 13, 2014 at 9:17
  • 1
    The normal distribution is “good enough” to quickly fudge some numbers, even when its usage isn't appropriate from a mathematical standpoint. Hence, my upvote. Generally, the model should dictate the distribution, but many things happen to be normal-distributed. For the question's parameters, there is a chance of p=5.5100800E-21 that one of the 800 villages will have negative population – absolutely negligible, but not nonexistent.
    – amon
    Commented Apr 13, 2014 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.