Friday 22 March 2024

Randomness

I gave a talk called "What is a Random number and why should I care?" a few times last year. It evolved each time, but I was fundamentally trying to decide whether I could define "randomness" properly. 

My first attempt was at ACCU. Unfortunately, in a surprising turn of events, my laptop died, but I did manage to give the talk using a pdf version on someone else's laptop. I got other chances at other conferences, for example MeetingCpp, so you can watch if you are interested. 

The second part of my title - why should I care - is easier to answer than the first. Without randomness, however we define this, outcomes are predictable, which is boring. For example, letting some blobs march up the screen at the same pace gives a very predictable outcome:


If each blob behaves randomly, more interesting things will happen, so we can watch them race.

Now, we can answer the first part of the question. What is a random number? There is no such thing as a random number, but a sequence of numbers might be random. If you can predict what comes next the numbers are not random, but if you can't that proves nothing. For example, what digit comes next in this sequence?
15926535897932384

It's a 6, because these are the digits of π, starting at the fourth digit. If you spotted that well done.

How do you generate random numbers on a computer? Most of the time, we use pseudo-random numbers. These are not random at all. They often use a congruence relation, generating a new number based on a previous number. The simplest is a multiplicative linear congruential generator
xi+1=AximodM


where A is a constant and M is a prime number. The first value of x is a seed, and using the same seed generates the same sequence of numbers. There are various other generators, some more complicated than others.

I still can't define random but using pseudo-random numbers is good enough for some machine learning applications or games. For example see my Swarm blog post, and I'll be talking about swarm algos at ACCU this year.

My first book, Genetic Algorithms and Machine Learning for Programmers, shows how to build some simulations and models using stochastic or (psuedo) random functions, and my latest book, Learn C++ by Example, aimed at helping people get back up to speed with C++, also has several simple games, like a higher/lower card game. To make the games fun, you get lots of practice using C++'s random numbers. The C++ book is currently on pre-order at Amazon, or you can buy it directly from the publishers.