Random Numbers Part 2: Residues, Groups, and the ZX Spectrum

It’s interesting how simple random number generators in computers such as the 1982 ZX Spectrum are:

\(X_{n} = 75^n \pmod{65537}\)

The nth term is equal to 75 to the power of n, modulo 65537. The first few terms are:

Index Term
0 1
1 75
2 5,625
3 28,653
4 51,791
5 17,642
6 12,410
7 13,232
8 9,345
9 45,505
10 4,951

There are 65536 terms in the sequence, before it resets and repeats from the beginning again. The ‘randomness’ is introduced by the modulus we’re working under - each time the power of 75 exceeds 65537, we wrap around and land somewhere “unexpected”.

For example:

\(X_{0} = 75^0 \pmod{65537} = 1\)

\(X_{1} = 75^1 \pmod{65537} = 75\)

\(X_{2} = 75^2 \pmod{65537} = 5625\)

These first three terms are just powers of 75.

However the next term isn’t just \(75^3\), because it would equal 421,875 which exceeds our modulus of 65,537. So taking the modulus (remainder of division of 421,075 by 65,537) we get:

\(X_{3} = 75^3 \pmod{65537} = 28,653\)

.. and so on.

These random numbers are constrained to the range \(1 >= X < 65537 \), and that’s because we’re working in modulo 65537 under multiplication (so we exclude 0). Just like hours on a clock face, it doesn’t matter what operations you do on the current hour, you always land on a new hour somewhere between 1 and 12. And in the case of our random number generator above, we can only produce any number from 1 to 65536. Bearing in mind the ZX Spectrum was released in 1982 and it only had 16 bit registers, this generator was a good choice to efficiently use the available hardware.

There’s a really interesting thing here. This random number generator will produce every number between 1 and 65536 only once, in “scrambled” order, before the sequence will repeat. This is just like shuffling the numbers from 1 to 65536, but in a really neat mathematical way.

How does it work?

Residues

It really helps to talk about some basic building blocks first.

In modular arithmetic, any integer corresponds to a residue in the particular modular system we’re looking at. For example, we could look at simple clock arithmetic with a 12-hour clock. The hours of the clock go 0, 1, 2, …, 11. There’s no 12 here as we consider 12 as being the same as 0. If we’re currently at hour 0 then where will the hour hand be in 24 hours? It will be at 0. And if the hand is currently at 1, then where will it be in 12 hours time? It will be at 1. In this way we can say:

  • 0 is the same as 0, 12, 24, … \(\pmod {12}\)

    • So, the residue class of \(0 \pmod {12}\) is \(0\).
  • 1 is the same as 1, 13, 25, … \(\pmod {12}\)

    • So, the residue class of \(1 \pmod {12}\) is \(1\).

and so on.

Because there are an infinite number of numbers which correspond to the same “hour” in our mod 12 system, we say that each of these sets corresponds to a residue, which is the representative of that set.

So we have only 11 residues in mod 12 arithmetic: \({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}\)

For each modulus \(m\), the set of residues has a size of \(m-1\). Each set of residues is just a set of numbers, and I emphasise this as we’re going to move to another concept which looks like a set of numbers but is more than that.

Groups

This will get a bit more abstract, but it will help understand how random number generators really work.

Groups are a fixed collection of objects (or just integers in our case) which obey these rules:

  1. There is an identity object
  2. There is an operation (e.g. multiplication)
  3. Applying the operation to any two objects in the group results in another object in the same group
  4. For each object in the group, there exists an inverse in the same group

In the context of our 12-hour clock example, there is only one multiplicative group:

\(\{1, 5, 7, 11\} \pmod {12}\)

We’ll explain the significance of this a bit later, but for now let’s look at its properties.

Looking at this group, it contains 4 numbers so we can only do operations with these 4 numbers. And since we’ve said this is a multiplicative group modulos 12,, the only operation we’re allowed to do is multiplication modulo 12. Let’s try a few things:

\(1 * 5 = 5 \pmod {12}\)

Multiplying 1 and 5 is allowed since they’re both in the group, which is multiplicative. The result we get is 5 which is within the group. We also discovered the identity element which is 1 - this is always the case under multiplication. So every multiplicative group under the integers must contain 1.

Let’s try another operation and see if we can break out of the group:

\(7 * 7 = 49 \equiv 1 \pmod {12}\)

\(7 * 7 = 49\) but in modulus 12 the residue is 1, which exists in our multiplicative group.

This is true for all elements of the group - any multiplications between elements will always result in an element from the group.

The final thing we need to know is about multiplicative inverses. In the same way that in regular arithmetic every integer has an inverse, e.g.:

\(2 * 1/2 = 1\)

The reciprocal of 2 is 1/2, and multiplying these together gives us the identity (1).

In modular arithmetic we also have the idea of an inverse of a number which can give us the identity. For this to be true for some element \(a\) from our group, there must be some other element \(b\) such that:

\(a.b = 1 \pmod {12}\)

In other words, multiplying our number \(a\) by its proposed inverse \(b\) would result in 1, the identity.

Let’s test this with our group above. First, the value 1 is trivial:

\(1.1 = 1 \pmod {12}\)

1 is its own multiplicative inverse. Now, what about the value 2? Well, we can try different candidates until we find one:

\(5.5 = 25 \equiv 1 \pmod {12}\)

So 5 is its own inverse, just like 1. Let’s try another:

\(7.7 = 49 \equiv 1 \pmod {12}\)

7 is also its own inverse. Let’s try the last one:

\(11.11 = 121 \equiv 1 \pmod {12}\)

11 is also its own multiplicative inverse. It doesn’t have to be the case that each number is its own inverse, but it can happen, and in this case it will be significant.

So what can we do with this? The first thing to know is that this group represents the “best” generator (and I won’t say random number generator) which modulus 12 can produce. It turns out to be pretty terrible - about as bad a generator as could exist.

Generators - well not quite …

If we take any element in this group and start taking its powers, we produce 1 on every odd power. For example for the element 5, we represent the generator for powers of 5 under modulus 12 as \(<5>\) and it works like this:

\(5^0 = 1 \pmod {12}\)

\(5^1 = 5 \pmod {12}\)

\(5^2 = 1 \pmod {12}\)

\(5^3 = 5 \pmod {12}\)

.. and so on.

We can only get the sequence 1, 5, 1, 5 which doesn’t look very random. The same is true for every other element in this group. So although we’re using the correct technique to make our generator, we’ve picked some really bad parameters.

It turns out that the reason it’s so bad is because every element in the group is its own multiplicative inverse. So the first time we multiply it by itself (square it) the answer is 1, which is the identity. Whenever we see the identity as a result, we’ve lost information. In the case of \(<5>\) the ‘information’ contained in 5 is lost each time we multiply it by its inverse, which is itself.

To round off this area, we’ll represent our multiplicative group the proper way:

\((\mathbb{Z}/12\mathbb{Z})^\times\)

This means the multiplicative group of integers (\(\mathbb{Z}\)) under modulus 12. This group happens to be known as the Klein four-group or \(V_4\) and is the smallest non-cyclic group in abstract algebra. A useful term came up there: non-cyclic. This points us to what we actually want: a cyclic group. A cyclic group under some modulus is one which contains all the residues under that modulus.

And the example of the ZX Spectrum is indeed based on a cyclic group, which is why it works the way it does. But how do we get there?

Towards a better group …

Firstly there’s something else which is good to know about a multiplicative group under some modulus. The elements in that group are the residues co-prime to the modulus. In other words, a residue belongs in the multiplicative group for a certain modulus if and only if the greatest common divisor of the residue and the modulus, is 1. So for residue \(a\) under modulus 12, a is in the multiplicative group if the following is true:

\(gcd(a, 12) = 1\)

This is why under modulus 12, the multiplicative group contains \(\{1, 5, 7, 11\}\) as these residues are coprime with the modulus 12.

So there’s a clue here: the sequence we can generate can only be as large as the size of the multiplicative group. So how can we maximise the size of the group?

Firstly, let’s use some proper terms. The size of a group is its order. So for our example above, the the generator \(<5>\) has order 2. Or: \(ord(<5>) = 2\).

How can we maximise the number of elements in the group? We’ve only really talked about modulus 12 so far, and we said that its multiplicative group is made from its residues co-prime to 12.

But what if we used a prime number as the modulus instead - how many residues would there be which are co-prime to that prime modulus? They all would be! This is because under a prime modulus, there is no way its residues can be multiplied to get to the modulus.

Prime Modulus and Primitive Roots

Let’s take modulus 13 as our next example. This has residues: \(\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\}\).

Are any of the residues a factor of 13? Naturally not as 13 is prime, so it has no factors. This means every residue is co-prime with the modulus 13, and we get a cyclic multiplicative group which we’ll call G:

\(G = \{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\}\)

This is a cyclic multiplicative group since it contains the entire set of residues modulus 13. Why do we say cyclic? Because there exists a generator which under multiplication (the group operation) can generate the entire group before repeating. Sounds familiar when we think about random number generators?

Can we see this in action? Let’s try some generators:

\(<2> = \{2, 4, 8, 3, 6, 12, 11, 9, 5, 10, 7, 1\}\)

… and the cycle continues from here.

Yes! This is starting to look like a true random number generator, though one with a short cycle.

So 2 is a generator of the cyclic multiplicative group G under modulus 13. Incidentally the very last term in the sequence is 1 and that’s always the case, because 7 is the inverse of 5 under modulus 13. So when the sequence hits 7 we multiply by 5 and get the identity 1, and the cycle repeats from here.

Is every element in G a generator for G?

Let’s try 3:

\(<3> = \{3, 9, 1\}\)

Interesting - this is not a generator of the cyclic group G, but a sub-set of it. We say the resulting group is aa sub-group of G.

One good bit of news though, is that for every prime modulus, there exists a cyclic multiplicative group. This means it contains an element which is a generator for the entire group. It turns out that the generator of the cyclic group modulus m is called the primitive root modulus m.

Sub Groups

Not every element of a cyclic multiplicative group is a primitive root. For example 1 can never be a primitive root, as it only generates 1 over and over! We might say \(ord(<1>) = 1\) for the multiplicative group under any modulus.

In a similar way, we saw that \(<3>\) did not generate the entire group under modulus 13 - in fact it generated a sub-group \(ord(<3>) = 3\).

It turns out that we can know some things about the order of groups and sub-groups in advance, given any modulus! Firstly, for any prime modulus \(m\) the cyclic multiplicative group exists, and it always has order \(m-1\).

But what can we say about the sub-groups? Lagrange’s Theorem says that the order of any sub-group) under modulus m must divide cleanly into the order of the multiplicative group! So, under modulus 13, the multiplicative group has order 12. And there may exist sub-groups of order 1, 2, 3, 4, 6 which are the factors of 12. Lagrange’s Theorem does not say whether these sub-groups exist, just that if they do exist their order must be a factor of the order of the multiplicative group.

For modulus 13 it turns out there is one sub-group of each possible order:

  1. The sub-group order 1: \(\{1\}\)
  2. The sub-group order 2: \(\{1, 12\}\)
  3. The sub-group order 3: \(\{1, 3, 9\}\)
  4. The sub-group order 4: \(\{1, 5, 8, 12\}\)
  5. The sub-group order 6: \(\{1, 3, 4, 9, 10, 12\}\)
  6. The cyclic group order 12: \(\{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\}\)

Putting it all together

So far we’ve learned some things to help us get towards a good random number generator.

Firstly, we need to use a prime modulus to ensure the cyclic multiplicative group exists. Otherwise we wouldn’t visit every residue under the chosen modulus.

Secondly, we need to use the primitive root of this cyclic group as a generator.

Going back to the ZX Spectrum example, this is based on:

\(X_{n} = 75^n \pmod{65537}\)

This is based on modulus 65537. The modulus is prime, so a multiplicative group exists with order 65536. We notice that in the equation above, we’re taking powers of 75. 75 happens to be the primitive root modulo 65537! So now we know how this generator works based on what we’ve learned this time.

Fermat Primes

But just one more thing … Why did the ZX Spectrum use the prime modulus 65537? Firstly, we know it has to be prime. And it’s large enough to work reasonably well.

First, consider that 65537 is a Fermat Prime. These have form \(2^k + 1\). Since they’re based on powers of 2 they work well in computing applications, as do Mersenne Primes which have form \(2^k - 1\).

65537 is \(65536 + 1\), which is \(2^{16} + 1\).

\(2^{16}\) represents the number of values which can fit in a 16 bit register. Convenient, since ZX Spectrum did have 16 bit registers, and this meant the random number generator used all the available precision in the 16 bit register.

As for how the ZX Spectrum RNG works in practice, it’s very efficient but that will be an article for another time!