![]() |
Country Generator 1.0.1
Procedural country generation for C++23
|
This guide covers every feature of the country-generator library in detail. For a quick overview, see the README. For the full API reference, run doxygen Doxyfile from the repository root and open doc/api/html/index.html.
dasmig/countrygen.hpp and dasmig/random.hpp into your include path.resources/ folder (containing full/ and/or lite/ subdirectories) so it is accessible at runtime.-std=c++23.The library ships two dataset tiers:
| Tier | Enum | Countries | Description |
|---|---|---|---|
| lite | dasmig::dataset::lite | ~195 | Sovereign states only |
| full | dasmig::dataset::full | ~250 | All countries and territories |
Resources are stored as resources/lite/countries.tsv and resources/full/countries.tsv.
On first access the singleton constructor probes these base paths:
| Priority | Base path |
|---|---|
| 1 | resources/ |
| 2 | ../resources/ |
| 3 | country-generator/resources/ |
It loads lite/countries.tsv if found, otherwise falls back to full/countries.tsv.
If your resources are elsewhere, call load() with a direct path:
Calling load() multiple times is safe — each call adds to the existing data. load(dataset) returns bool (true if a matching file was found and loaded). load(path) returns void and silently does nothing if the path does not exist.
Every generated country includes all 31 data fields:
| Field | Type | Description |
|---|---|---|
cca2 | string | ISO 3166-1 alpha-2 code |
cca3 | string | ISO 3166-1 alpha-3 code |
ccn3 | string | ISO 3166-1 numeric code |
name_common | string | Common English name |
name_official | string | Official English name |
capital | string | Capital city (;-separated if multiple) |
region | string | UN geoscheme region |
subregion | string | UN geoscheme subregion |
continent | string | Continent (;-separated if multiple) |
latitude | double | WGS84 decimal degrees |
longitude | double | WGS84 decimal degrees |
area | uint64_t | Total land area in km² |
population | uint64_t | Total population |
landlocked | bool | Whether landlocked |
independent | bool | Sovereign state flag |
un_member | bool | UN membership |
languages | string | ;-separated language names |
currency_code | string | Primary ISO 4217 code |
currency_name | string | Primary currency name |
currency_symbol | string | Primary currency symbol |
borders | string | ;-separated alpha-3 border codes |
timezones | string | ;-separated UTC offsets |
driving_side | string | "right" or "left" |
tld | string | Country-code TLD |
idd_root | string | IDD root (e.g. "+5") |
idd_suffix | string | IDD primary suffix |
demonym_m | string | English male demonym |
demonym_f | string | English female demonym |
flag_emoji | string | Unicode flag emoji |
income_level | string | World Bank income level |
start_of_week | string | "monday", "sunday", or "saturday" |
Pass a UN geoscheme region to restrict generation:
Available regions: Africa, Americas, Antarctic, Asia, Europe, Oceania.
Region-filtered selection is also population-weighted within the region. Throws std::invalid_argument if no countries match the region.
By default, countries are selected with probability proportional to their population. China and India are far more likely to appear than Liechtenstein. Countries with zero population are assigned a minimum weight of 1.
Switch to equal-probability selection where every loaded country has the same chance of being picked, regardless of population:
The weighted() setter returns *this for chaining:
Query the current mode with:
Pass an explicit seed to get_country() to produce the same country every time:
Every country records its seed. Retrieve it with country::seed() and pass it back to reproduce the exact same result:
Seed the generator's engine for a reproducible sequence:
Note: To replay a region-filtered country, pass both the region and the seed:
gen.get_country("Americas", c.seed()). Using justc.seed()without the region filter will select from the full dataset and likely produce a different country.
Construct independent generators with their own data and engine:
The shipped datasets are generated from open data sources using the included Python script:
This produces resources/full/countries.tsv and resources/lite/countries.tsv.
Data sources are licensed under ODbL, CC BY 4.0, and open source licenses. See LICENSE_DATA.txt for details.
Each cntg instance is independent. The static instance() singleton uses a local static for safe initialization.
| Operation | Thread-safe? |
|---|---|
instance() | Yes (static local) |
get_country() on different instances | Yes |
get_country() on the same instance | No — requires external synchronization |
load() | No — must not be called concurrently with get_country() on the same instance |
Call load() once during initialization before spawning threads. For concurrent generation, give each thread its own cntg instance.
| Exception | Condition |
|---|---|
std::runtime_error | get_country() called with no data loaded |
std::invalid_argument | get_country(region) with unknown region |