In the last post, I began to outline how I planned to create realms and settlements. Realms and settlements are hierarchically linked so that every settlement belongs to a realm. Reality of course may be different but my purposes this is a good place to start.

Today I want to start generating my realm's cities and spreading the population evenly to make it look organic.

### Credit where it's due

I'm indebted to the outstanding work of S. John Ross and his extremely popular and influential article, *Medieval Demographics Made Easy*. Although I will use different methods for calculating values (Ross relies heavily on dice mechanics, for example, and I'm not a gamer), I'll be generating much of the same data using principles he has defined.

There's also a number of generators available on the web, that are direct implementations of or are inspired by Ross's work. Most are either JavaScript powered HTML forms or Microsoft Excel documents. When I first encountered Ross's article, I took an early stab at creating a spread sheet version using Apple Numbers and used a very basic PHP script for programming logic and looping.

As with the rest of my world building application however, I'll use PHP to do the heavy lifting and JSON to store data.

### Generating text

At some point in the future, I want to incorporate text generators to handle the creation of random names and phrases. I will most likely use some kind of Markov chain inspired algorithm but that's a task for another day. Until then I'm going use a simple sequence for names, particularly when I'm creating content in a loop.

### Population

In Ross's article, the fundamental metrics he uses to generate a realm is the area and the population density. The area of a realm is circumscribed by geography and political boundaries and notwithstanding military conquests, major natural disasters, global warming or huge engineering projects is unlikely to change.

Population density is a different animal because it allows us to define the carrying capacity of your realm based on available arable land and the farming technology able to exploit it. Ross explains the variations in detail but basically, the flatter, more arable and more clement your realm the more people you can sustain. Keep in mind that medieval agriculture was pretty labour intensive and inefficient and even highly arable countries could expect a population density of no more than 50 people per square mile as an absolute minimum.

In my last post, I already note that when I create a realm, I specify both the area and population density. If you recall however, I also record:

- The level of urbanisation
- The threshold population of urban settlements

#### Urbanisation

Urbanisation is the percentage of a country's population who live in urban settlements such as cities and large towns. It's used by demographers, economists, archaeologists and historians as one of many measurements of a country's state of economic development.

Cities are complex. They often outgrow their hinterland's ability to provide enough food for the populace so they rely on trade. They require administration, planning, taxation, defence etc etc. They are centres of political power, specialised industries, art, religion and are major nodes on transportation networks.

Our modern levels of urbanistion where only made possible when the industrial revolution freed labour from agriculture and concentrated manufacturing in cities. Great Britain, the world's first urbanised and industrialised country did not exceed an urbanisation level greater 20% until the 19^{th}C.

In an early medieval realm, urbanisation might be as low as 3-5% of the population. In a late medieval or early modern realm with improved agricultural technology might be as high as 20%. The exceptions are city-states, where everyone lives in the city for protection and commutes to the fields (as was the case in Ancient Sumer). In these example urbanisation might be as high as 90%.

### Creating our Capital City

Like Ross, I'll start with the realm's capital city and use that as a benchmark for the generation of the remaining urban settlements. In most historical cases, a realm's capital city is going to be it's largest (often by a significant margin). Centres of power and population are intricately linked; it's no coincidence that the largest cities in Europe are centres of trade, politics, religion, art and innovation were centres of royal and imperial power.

In Ross's article, he calculates the population of a capital using the following formula:

```
pop = sqrt(P) * M
```

P is the realm's population and M is the result of a random roll of two four-sided die + 10 (2d4+10).

He doesn't explain where he derived this formula but I think it produces good results for the majority of realms because the result isn't too top heavy. By that I mean that it's unlikely to produce very large cities, which is more realistic given the limits of agriculture and transportation in your typical medieval economy. There's no harm in using this formula, particularly if you are a gamer and you like dice mechanics.

Turning this formula into a basic PHP script looks something like this:

```
total_pop = 3250000;
city_pop = (sqrt(total_pop)) * (mt_rand(1,6) + mt_rand(1,6) + 10);
```

The number I chose, 3.25 million is roughly the population of 11thC England. England occupies an area of roughly 130,395 square kilometres giving a population density of 25 people per square kilometre. This is exactly what I would except from what is a hilly, temperate and pre-industrial country that was heavily forested.

So I decided to test this formula and spit out 10 results:

- 27,042
- 28,844
- 27,042
- 36,056
- 30,647
- 34,253
- 25,239
- 32,450
- 37,858
- 37,858

There's actually not much variation in the output; in this example I used MS Excel and I suspect the RANDBETWEEN() function isn't very random and besides, our multiplier will only ever be between 12 and 22. Moreover the results do not reflect historical examples nor can it factor in trends that affect population. For example, London's 11^{th}C population was roughly 11,000, nearly a third lower than the calculated amount. Using one dice or halving 10 to 5 gets us closer to the number but it still feels like this formula has been plucked out of thin air.

So let's try another approach, replacing the dice mechanics with a few variables we introduced during my previous post.

We already know our total population, in this example 3.25 million, and if we set our urbanisation level at 5% we get an urban population pool of 162,500. From here the easiest way to set the capital city's population is to specify a percentage.

For example, if we say our capital city has 7% of our urban population pool we get a figure of 11,373, which is pretty close to London's 1100 A.D population. By using this method, we have more control over the final number and can factor in socio-political trends. Increasing that percentage means we are in effect increasing the centralisation of the realm. This is exactly what happened in England from 1100 to 1350; London's population increased from 11,000 to 80,000 while the overall population of England experienced a net decline due to the ravages of the plague. London grew in prestige and wealth thanks to the burgeoning wool industry, royal patronage and increasing manufacturing and thus it attracted migration from rural England.

Similar trends of population centralisation occured in the city-states of Ancient Sumer and Greece. In fact this method copes well with city-states because all we have to do is apportion a greater percentage of the urban pool to the capital city. If we want to add some randomness we can do so by selecting a random number between a range of percentages. For example:

- 80-100% - a city-state
- 50-79% - highly centralised nation state
- 30-49% - centralised national state
- 5-29% - dispersed national state

Note that in specifying a range of 80-100% for city-states we create the possibility of a secondary city, like Athens' port city of Piraeus for example.

### Generating other settlements

The other benefit to setting the level of urbanisation is that we can cap the population of all cities and towns. It's easy to calculate the total urban population pool and, by subtracting it from the national population we get the rest of rural population.

Once we subtract the population of the capital city from the urban population pool, we are left with a pool of people we need to scatter around the realm in other lesser cities and towns.

So how big should they be? How big is a city or a town.

The answer is up to you but once decided we can define the upper and lower limits of our regular cities and towns. For the sake of argument let's say this is 12,000 and 1,000. This means that, excluding our capital city, all other urban settlements have populations that fall in this range. While we are at it, we might as well specify a threshold that distinguishes a city from a town, let's say that's 2500.

Anything that's smaller than our lower limit of 1000, we count as a village and won't bother with today.

Now that we have our capital city and our thresholds, it's time to generate the populations rest of the cities and towns. Again I'll start with Ross's articles which proposes and elegant approach.

Ross calculates the population of the next largest city as a random percentage (20-80%) of the capital city. Then each remaining city is calculated as having a population between 10-40% smaller than each proceeding city. You simply repeat this process for as long as you can sustain a population above the city's population threshold. Once you reach this limit, Ross suggests a different method to calculate the number of towns and once that's less labour intensive (mostly likely because he's working with a dice and calculator rather than a computer).

This kind of reductive process is really valuable because it's lets us create a population spread that accurately reflects medieval populations. It's crying out though for a little automation and by automating the process we can also calculate towns too; even if there's a thousand towns a computer will generate them all in seconds, even using a clunky scripting language like PHP.

In my approach, I'll use an upper limit (the remaining urban population) to kick off a while loop that does the business of creating a random population until that population pool is exhausted. In the following example, I'll assume that I have a capital city of 11,500 remaining population pool of 100,000 and that I'm using different parameters to calculate the first city in the loop than all subsequent settlements.

```
function calculate_city_population($limit, $min, $max)
{
$multiplier = mt_rand($min,$max);
$population = $limit * ($multiplier/100);
return $population;
}
$urban_pool = 100000; //our population pool
$i = 1; //sequencer
$allocated_pool = 0;
$capital_city_pop = 11500;
$city_upper_limit = 10000;
$city_lower_limit = 1000;
while ($allocated_pool <= $urban_pool)
{
if ($i == 1)
{
//calculate second city pop: 20-80% of the capital city
$city_pop = calculate_city_population($capital_city_pop, 20, 80);
} else
{
//subsequent city: 10-40% smaller than each proceeding city
$city_pop = calculate_city_population($city_pop, 10, 40);
//hack to prevent settlements under 1000 being generated
if ($city_pop < $city_lower_limit)
{
$city_pop = $city_pop + $city_lower_limit;
}
}
echo "The population of city " . $i . " is: " . round($city_pop) . " \n";
$i++;
$allocated_pool = $allocated_pool + round($city_pop);
}
echo "The allocated pool is " . $allocated_pool . PHP_EOL;
```

When I run this script I get 73 settlements with an output looking like this:

```
The population of city 1 is: 3680
The population of city 2 is: 1662
The population of city 3 is: 1632
The population of city 4 is: 1228
The population of city 5 is: 1184
The population of city 6 is: 1213
The population of city 7 is: 1267
The population of city 8 is: 1241
The population of city 9 is: 1484
The population of city 10 is: 1490
~
The population of city 73 is: 1447
```

The allocated pool, based on the sum of the settlement populations, is 101,071 which is pretty close to our initial pool. However of the 73 settlements only one is above 1000 people; so it means we have a lot of towns but very fews cities. To tweak this, I can simply calculate more cities based on the first formula. If I choose 5, I get the following output:

```
The population of city 1 is: 4485
The population of city 2 is: 3565
The population of city 3 is: 9085
The population of city 4 is: 4025
The population of city 5 is: 4945
The population of city 6 is: 1385
The population of city 7 is: 1485
The population of city 8 is: 1490
The population of city 9 is: 1402
The population of city 10 is: 1196
~
The population of city 62 is: 1336
The allocated pool is 100404
```

Now I have 5 new cities with a healthy population mix. If I want to aim for approximately 30 cities and towns analogous to English medieval populations I would to calculate 12 cities using the first formula.

```
The population of city 1 is: 3220
The population of city 2 is: 7590
The population of city 3 is: 4255
The population of city 4 is: 6555
The population of city 5 is: 5865
The population of city 6 is: 2645
The population of city 7 is: 4600
The population of city 8 is: 8970
The population of city 9 is: 6210
The population of city 10 is: 7245
The population of city 11 is: 8050
The population of city 12 is: 5290
~
The population of city 34 is: 1175
The allocated pool is 100259
```

The point is that I can tweak the process to give me different results. Calculating only 1 settlement at the first formula will create a lot more towns and much fewer cities; something you'd expect in less advanced or highly decentralised countries. Calculating more means were are more likely to get larger and fewer settlements thus concentrating our urban population.

There are social implications to this that will shape our stories and the character of our world. More cities means more centres of power, administration and levels of complexity and trade i.e. a realm with the characteristics of an Early Modern society.

### What's next?

In this post, I wanted to create a flexible ways of generating settlements. Calculating and spreading populations that are inspired by historical models. So far I've focussed on generating settlements using my population pool. Next post, I'll be looking to use that information to calculate or generate more details.