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 x
_{0}. - Generate a sequence of numbers using the congruence x
_{n+1}= (ax_{n}+ c) % m

We can try this with m = 9, a = 7, c = 4, and x_{0} = 3:

x_{1}= (7x_{0}+ 4) % 9 = (7 * 3 + 4) % 9 = 25 % 9 = 7 x_{2}= (7x_{1}+ 4) % 9 = (7 * 7 + 4) % 9 = 53 % 9 = 8 x_{3}= (7x_{2}+ 4) % 9 = (7 * 8 + 4) % 9 = 60 % 9 = 6 x_{4}= (7x_{3}+ 4) % 9 = (7 * 6 + 4) % 9 = 46 % 9 = 1 x_{5}= (7x_{4}+ 4) % 9 = (7 * 1 + 4) % 9 = 11 % 9 = 2 x_{6}= (7x_{5}+ 4) % 9 = (7 * 2 + 4) % 9 = 18 % 9 = 0 x_{7}= (7x_{6}+ 4) % 9 = (7 * 0 + 4) % 9 = 4 % 9 = 4 x_{8}= (7x_{7}+ 4) % 9 = (7 * 4 + 4) % 9 = 32 % 9 = 5 x_{9}= (7x_{8}+ 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 = 2^{31} - 1, and a = 7^{5}. With these values, 2^{31} - 2 numbers are generated before there is repetition, and this is enough for most uses.