Math for Music Games

Mathematics understood as proving theorems will not be our consideration. You may be inspired by any field to incorporate it in your music games, here however, let's list specific areas of math for a general use in music game design.

Disciplinary shout-outs

Tuning system is a topic that shows up the quickest when talking about math in music. It's an ancient observation that you need to shorten the musical string in half to arrive at the octave from the open string. Pythagoreans concluded that most "rounded" ratios (1:2, 1:3…) give the most pleasant intervals (consonance) and built the system of music around their investigations. Currently most popular system, 12-tone temperament, resolved the matter of pitches within the octave similarly but differently by using different math.

Using tuning systems as a music game mechanic is a challenge, because it requires instruments not fixed within a specific tuning, and high level of listening skill from the players. As an area of exploration it might be most promising as an app-assisted solo game (with a tuner).

Downtime — A situation during the game when a player is not engaged in play.

Probability, on the other hand, is the math subfield that finds maybe the most usage in games. Not only may it be used by players, but designers often wish to check how frequently some situations during the game might happen. For example when you use the deck of cards, and playtesting showed that having two specific cards together in a starting hand leads to sad downtime, you have a choice as a designer:

A) remake a portion of the game (a lot of work)
B) add a trouble-shooting, inelegant rule to avoid the bad situation, or
C) accept that on average one in N players might be reasonably critical towards the game.

But what exactly is this N? It depends on the size of the deck (D) and the amount of cards drawn at the start (H). Before calculating, you may leave the purity of mathematics for a moment and consider using a Monte Carlo method, which is a repeated simulation of the problem at hand. It's often done with a short program, but might be attempted with a spreadsheet. If you generate 1000 random hands drawn from your deck, you will arrive at the estimated probability for your situation. It's often useful, because you may easily check the chance of more complex situations than our relatively simple example.

\begin{align} N = \frac{D(D-1)}{H(H-1)} \end{align}

The result for N (provided above without explanation, sorry, on the basis of probability theory) means that if you deal 7 cards from a standard 52-card deck. One in about 63 players will have a sub-par experience in our considered game. Unfortunately, neither mathematics nor game design will tell you how to use this information, and which of the A-C options should be chosen.

Integer sequences

For areas of mathematics useful more specifically in music games, let's start with facts drawn from number theory. Integers ("whole numbers") form some of the well known sequences used in music and games. Fibonacci sequence is one of those, and it's the one where the next item is a sum of two previous ones, starting with two 1s, so it's:

1, 1, 2, 3, 4, 7, 11, 18…

It will serve as a useful example soon, let's not go into details, but the Fibonacci sequence was used so often in music, arts and memes, that it provoked one of the Composition Exercises to instruct:

Don't use a Fibonacci sequence

Another sequence that only grows is beloved by game designers. Triangular numbers increase by minimally larger amount on each step going: 1, 3, 6, 10, 15… These are often used as points benchmarks because their growth is slow and steady and nicely represents the increase in the challenge that goes in sync with the development in the gameplay.

Providing for players such a task about numbers growing with an algorithm will test their memory and skill in arithmetic. But playing over such monotone sequences, won't last long with non-specialists, unless they are given tokens or other aids. There is seemingly more potential in using sequences that may both grow and diminish. They not only are more handy, but may behave in a quite surprising manner. Let's see how this could be achieved and a bit of formality may help us now (not much).

We will deal with integer sequences defined recursively. It means that we show how to calculate the value of the next number on our list by relating it to the previous numbers. To do this we'll use symbols $a_1, a_2, a_n$ which will mean the first, second, n-th number of the sequence respectively. If we want to know the value of $a_n$ we may need to use the value of the previous number in the sequence, $a_{n-1}$. For example, here is the rule for the Fibonacci sequence1:

\begin{equation} a_n=a_{n-1}+a_{n-2} \end{equation}

Two earlier numbers are to be summed into our current number, so after establishing that $a_1=1, a_2=1$ we'll have a Fibonacci sequence considered in its infinite entirety. For comparison the rule for triangular numbers will be:

\begin{equation} a_n=a_{n-1}+n \end{equation}

Now let's look at:

\begin{align} a_{n+1} = \left\{ \begin{array}{ll} \frac{a_{n}}{2} \ \ \text{for even } a_n \\ 3a_{n}+1 \ \ \text{for odd } a_n \end{array} \right. \end{align}

The above equation sets the value of the "current" sequence element based on the value of the previous elements. It uses a condition, requiring a different calculation for odd and even base numbers. It defines not only a single sequence, but multiple (depending on the value of the first element). Consider the result when starting from a few first numbers.

1 (odd, so we use 3n+1 rule), 4 (even, let's halve it), 2 (halve), 1, 4, 2, 1, …
2, 1, 4, 2, 1, 4, …
3, 10, 5, 16, 8, 4, 2, 1, …
4, 2, 1, 4, 2, 1, …
5, 16, 8, 4, 2, 1, …

You may spot a few recognizable patterns. As the assignment is deterministic, whenever it stumbles upon an already familiar number it will always have a known further sequence, reaching 1 in all of the examples above, and then looping between 4, 2, and 1 again. This is actually an open (and "cursed") area of mathematics, to prove if all starting numbers follow the algorithm towards 1. The humanity doesn't know the answer yet, although huge amounts of numbers were already checked and confirmed to always reach 1.

This is called Collatz problem and if it doesn't seem interesting for you yet, let's investigate the length of the pattern as the number of steps from the starting number to the first occurrence of 1. As you see above, so far we had lengths of 0, 1, 7, 2 for starting numbers 1 to 4. The lengths for next starting numbers are 5, 8, 16, and 3.


For all 16 steps of the sequence when starting with 7, the values jumped up to 52. And just for our viewing pleasure, let's see what happens if you start a Collatz sequence from 27, which is pictured on the graph to the right — a long run with huge leaps (over 9000), right? Did you expect this (seeing it for the first time)? This demonstration may point to one potential usage of mathematics to explore: to help us generate results that are on one hand relatively "unpredictable" to the naked eye, but also deterministic, and guided by simple rules. In short, it helps to achieve emergent complexity. We will stay with the effect of this type for two more sections, before moving to different design opportunities.

Modular arithmetic


In modular arithmetic you go back to 0 upon reaching your modulus (the number of numbers that you operate with, i.e. a one-upper of your maximum number). The usual example is a clock, where (with a modulus 12) you arrive at 4 after adding 6 to 10.

\begin{equation} 10 +_{12} 6 = 4 \end{equation}

You can use a modulus of 12 or 24 or 60 for clock time, but you can use 12 also if you would like to translate a chromatic scale to numbers. In practice, it's often not the simplest mental arithmetic to evaluate a remainder of a division, for this purpose moduli of 5 or 10 are the most handy. Another workaround may be needed for the value "zero", present in modular arithmetic, but not typically numbered in music (where the count usually starts with 1). But already some amusing results might be achieved with "number of notes" parameter, where zero is allowed, like in this example.

Modulo Game

Establish a conductor before the game.

Conductor: You will signal the entries for 5 rounds. In each round show the tempo and the downbeat according to the conventions within your genre. Agree on this signal with players and demonstrate, it might be done with two sharp nods, with the intention that sounds start after the second nod, following the tempo of nodding.
Before the first round, tell the players two "seeding" numbers of your choice in 0-4 range. For example "3 and 3" or "4 and 0" — the order of non-equal numbers is important.
Each round will last at least 5 beats — you can check how busy the players are if you want to give them comfortable time. After 5 rounds, signal that the game is over.

Players: In every round you will play X even notes in a tempo given by the conductor for this round. Pitches and timbres are up to you. Each time, the X is the same for every player and your main goal in the game is to play the notes in sync and without mistake in the length of your phrase. The calculation for your X starts with numbers given by the conductor, we'll call them A and B.

You calculate X in the following way:

  1. increase A by 2
  2. increase B by 1
  3. multiply A and B and increase by 2; C=A*B+2
  4. X is a remainder of C divided by 5.

For every next round you will use previous B as your new A, and just calculated number X as your new B. If in doubt, please see the examples.

Cellular automata

The next method of planting chaos into your games doesn't even use numbers. Cellular automata were popular in mathematics since after the war, and they are also close to the pioneers of informatics. The basic form of such an automaton is a collection of cells with at least two states, usually "on" and "off" and rules on switching the state according to the state of "neighboring" cells. The spatial relations between cells might differ between specific automatons. In each computed "time step", every cell sets its next state independently and rules should be deterministic (nothing at random). Each time a cell has the same situation (in its surroundings and state), it will behave in the same way.

This few features leave a lot of possibilities of configuration that we will now examine in relation to potential music-making usage. If you are interested further in the basic topic, the most famous historical example to check elsewhere will be Game of Life. The most notable examples of automatons share the feature of providing interesting behavior of the whole system, while being based on a simple rules.

The most straightforward application of CA for music could be constellation games (ones that don't limit the played material, only the line-up). Here, let's consider a player to be a cell with two states: "playing" and "not playing". There will be N players standing in the circle. Everyone has only two "neighboring cells": left and right. Of course when constructing your own system, you may place players in many ways (change the number of neighbors) and give them more states (like "sitting" etc.). But if you want to keep it a proper cellular automaton, you should aim for conformity in your setup, players at the edge of the orchestra shouldn't play by different rules than those in the middle.

There is one last consideration that is important for Cellular Automata: starting pattern. When you run your rules from the state of all cells being "off" it is impossible to arrive at any interesting result. When all cells behave exactly the same, your only options are "staying off", "turn on and stay that way", or some form "blinking together". But if you have one cell "on", you already have three types of cells: "on", "off and away", and "off in the neighborhood of on". Still, a single cell is a perfectly symmetrical pattern, and naturally, the asymmetrical ones will yield more interesting results.

Cellular Constellation

Players are in the circle, one conductor is in the middle (or otherwise visible to all).

Conductor: Before the first round, point to some players (maybe 20% of them) that will be playing at the start. During playing, you will signal round changes to the ensemble for about 20 rounds (extending possible if everyone (!) is having fun). After that, signal the ending of the game.

Player: There will be rounds, signaled by the conductor, but not everyone is playing all the time. You play in the starting line-up if the conductor points at you just before the first round. Then, you follow these rules:

  1. If you are not playing now, play in the next round if any of your neighbor is playing now.
  2. If you are playing now, stop playing in the next round if any of your neighbor is playing now.

In other words:

If any of your neighbors is playing, change your playing state in the next round.

The length of such a game is arbitrary, not only because the timing depends on the conductor, but the number of rounds could theoretically be set at any number. Nevertheless, if we were to continue this game indefinitely, it is certain that at some point we repeat the constellation that have already played and, because the rules are constant, the game would have to proceed in the same way as before. You can experiment with many rules, player counts, and starting constellations in the Constellation Game app.

For example, if you set the single rule as in our example above. And have a "play-pause-play-play" starting constellation (a smallest non-symmetrical pattern), during the course of the game you will have 172 unique constellations and then the algorithm will loop by repeating constellations that already happened. Here is what you need to set up for this result, before clicking the "run" button:


Graph theory

Discreet elements and links between them is the domain of graph theory. It can be used to analyze the situation of players (and their possible reacting to each other), but more often the nodes of the graph represent sections of the piece and an edge between the nodes mean the transition between them. For maybe the best example in the wiki library, see: Apocalypse. Such structures are in fact graphs, but they might be casually considered as "maps", "schemas", etc.

Graph theory may first and foremost provide many pieces of useful vocabulary ("directed" graph, "planar" graph, "bipartite" graph) and some methods of achieving wanted features within a graph. The field is vast, so a few examples presented are honorable mentions, given without proof.

— You cannot cross all pictured bridges without repeating the trip on some of them (this was, by the way, a puzzle that started the whole theory),
— You can color a map with only 4 colors,
— You can draw a graph without crossing its lines, if it doesn't contain any of the subgraphs seen below:

Basis of graph theory seems far from the usual domain of math: numbers. But these are incorporated swiftly, most naturally by counting how many edges are connected to a given node, or by "weighting" the edges, or by considering numbers of colors needed to achieve a specific coloring.

Game theory

We will finish this presentation with a relatively new, post-war theory that combines graphs and probability at it's basis, but soon arrives at the usage of matrices and some other areas of advanced mathematics. Prisoner's Dilemma is maybe the most famous example from this theory and it will serve us here too.

The story goes: two thieves were caught and there's evidence against them that will make each serve 1 year in prison. Each thieve is separately given an offer: "Testify, and if your accomplice doesn't, you may go free". Denying guilt when you are denounced will put you in prison for 3 years and when both confess it end in 2 years for each. Vanilla game theory says this last option is the solution of the game. First, let's see how the notation for this game might look like:

(-1, -1) (-3, 0)
(0, -3) (-2, -2)

What does this mean? How to read this table?
Players. Firstly it's a 2-player game, which is recognized by the number of dimensions for the matrix. One player chooses between rows, the other has columns (if we needed a third player, the 3-dimentional cube would represent the game). In game theory it's assumed that each player is rational (plays optimally).
Strategies. In this example each player chooses between two options — strategies. In our simple example, the difference between choice and strategy is not visible. But if during the game there are more choices, a strategy will be defined as a plan on what to do in any situation.
Result. The string of numbers in the cell (a "vector") shows gain or loss of each player. "(-1, -1)" in top-left means that both players get -1 if their both first options are chosen. "(-3, 0)" to the right means that the first player (choosing rows) will loose 3, and the second will get 0 — that will be the result if the first player chooses the first row, and the second player will choose the second column.
Payoffs. The example table shows "years in prison", but the number in the result has a more strict interpretation. It shows utility, which is a precise numerical representation of preferences. Every value in the table is known to the players beforehand.

The matrix presentation of the game is called a "normal form". Based on the defining elements shown above, there are many additional terms introduced in the theory, for further analyses.

  • Dominant and dominated strategies — is a pair of strategies where one always gives better result than others (the dominated will not be used by a rational player).
  • Optimal strategy — is the strategy that gives best payoffs for the player.
  • Mixed strategy — is when a player uses two or more strategies decided at random. Sometimes, none of the pure strategies are optimal, but when probability is assigned to each option, it gives the highest expected value. A mixed strategy is then optimal.

The most notable use of game theory for music is by Iannis Xenakis, described in detail in the review of the book Formalized Music. Thought and Mathematics in Composition.

Other connections

Oumupo is a community of composers in France who explore the connection between mathematics and music in their individual practice.

Scratch Orchestra's CCQR133 is currently the only in-library game with connection to mathematics. — Online Encyclopaedia of Integer Sequences lets you explore the sound of the sequences presented. — algorithmic generation of music.

If you think anything should be added to this subpage, please drop a hint or a link for future editors.

Unless stated otherwise Content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License. See licensing details