C# Random – How to Create a Set of Random Numbers Based on a Total and Number of Objects

crandom

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.

Best Answer

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)
print(vil)
print(sum(vil))

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