update: Removed magic scalar from supply and demand calculations.

This is more of a note to be expanded on later. However, I believe some will find it useful in this raw form.

Many interesting games, like M.U.L.E. and Trade Wars, involve the mechanic of supply and demand. To model this in code, where you calculate demand and supply to arrive at a price, can be done through the following forumlae.

Calculate demand: This is usually a simple count of your potential consumers. Perhaps the number of players will equal demand. This depends on the context of your game. This number will be referred to as ‘d’.

Calculate supply: This is a simple count of the number of widgets available that are to be sold. Again, this is contextual. This will be called ’s’.

Calculate relative demand: You will need to pick an arbitrary equilibrium number that presents the perfect balance between supply and demand. This number is going to be arbitrary, so let’s use 100.

Relative Demand = (d / (100 + d))

Calculate relative supply: This looks a lot like demand:

Relative Supply = (s / 100 + s))

Calculate price: You will pick a price that represents the price at equilibrium. Here, that is 100 currency units. Your number will be different:

Price = 100 * (Relative Demand / Relative Supply)

You must code around the case where supply is 0. The price becomes not-a-number.

The result of these maths is that when demand is high and supply is low, the price increases over some baseline. When the supply is higher than demand, the price lowers. That’s mostly how life works.

These formulae will give you a very linear relationship between supply and demand. You may need to tweak this for your game. Inverse logs might be more realistic or even exponential functions. This is most certainly an exercise for the reader.

Please note that this supply and demand model is far from perfect. Most economists will tell you that the variable to look at is price, which then controls supply and demand (this might be a “freshwater” bias). However, most games will want to compute a price rather than predict supply and demand.