random numbers

You've all probably encountered the random() function in Processing. Although thinking of a random number seems trivial for a human, this is actually a fairly complicated task for a computer. This is because a computer is a calculator; all it knows how to do is count and perform operations according to predefined rules, and just picking some value arbitrarily is not something it can do. It needs to use a systematic method, and this is why machine-based random number generators are actually referred to as pseudo-random number generators.

There are many techniques for generating pseudo-random numbers, and the most commonly used is the linear congruential method:

• Choose 4 integers: the modulus m, the multiplier a, the increment c, and the seed x0.
• Generate a sequence of numbers using the congruence xn+1 = (axn + c) % m

We can try this with m = 9, a = 7, c = 4, and x0 = 3:

```x1 = (7x0 + 4) % 9 = (7 * 3 + 4) % 9 = 25 % 9 = 7
x2 = (7x1 + 4) % 9 = (7 * 7 + 4) % 9 = 53 % 9 = 8
x3 = (7x2 + 4) % 9 = (7 * 8 + 4) % 9 = 60 % 9 = 6
x4 = (7x3 + 4) % 9 = (7 * 6 + 4) % 9 = 46 % 9 = 1
x5 = (7x4 + 4) % 9 = (7 * 1 + 4) % 9 = 11 % 9 = 2
x6 = (7x5 + 4) % 9 = (7 * 2 + 4) % 9 = 18 % 9 = 0
x7 = (7x6 + 4) % 9 = (7 * 0 + 4) % 9 =  4 % 9 = 4
x8 = (7x7 + 4) % 9 = (7 * 4 + 4) % 9 = 32 % 9 = 5
x9 = (7x8 + 4) % 9 = (7 * 5 + 4) % 9 = 39 % 9 = 3
```

which generates the repeating sequence 3, 7, 8, 6, 1, 2, 0, 4, 5, 3, 7, 8, 6, ... This is a good example but is too short to be practical. A common congruence has c = 0, m = 231 - 1, and a = 75. With these values, 231 - 2 numbers are generated before there is repetition, and this is enough for most uses.

Math For Artists Who Now Need to Program

Elie Zananiri
ITP DriveBy
1 Oct 2008