Hello again! In this video, we are going to start looking at random numbers. A random number is one
which appears as though it has been chosen by chance.
Usually we have a range of numbers and we assume that any number in this range is equally likely to
be chosen.
So for example, if you have a pack of cards - playing cards - and you choose one at random, it is equally
likely to be any one of the 52 possibilities.
And that is what mathematicians call a "uniform distribution".
We also assume that the next number we choose is not affected by the last number we chose.
So the choices are "independent".
So that is like tossing a coin.
If you toss a coin and get heads, then you still have a 50-50 chance of getting heads or tails
the next time.
Random numbers are actually very useful in computing.
The obvious example is in games. For example, if you have a card game, then you can deal the player
a different hand every time.
If you have an action game, then you can make the opponent move around unpredictably, which makes
him harder to catch.
If you have a game with a story, then you can choose different branches through the story.
So random numbers will make games more interesting and unpredictable.
Random numbers are also used in numerical computation. For example, in "Monte Carlo" simulations.
If you do not have complete data, you can put in random numbers with sensible values.
Then run the simulation lots of times, and take an average. And that should give you some idea of what is
going to happen.
So that is basically how weather forecasting is done,
for example. In cryptography, we can use random numbers to create codes and ciphers, which are unbreakable.
Or very hard to break, anyway!
And they are also used in authentication for network security and crypto.
How do we get random numbers? If we are using software, we have a bit of a problem, because software is
deterministic, so it cannot do things by chance.
However, there are things called "pseudo-random number generators".
These are algorithms which will do some calculations, and that will generate a sequence of numbers which
look fairly close to being random.
Obviously, it will produce the same results every time.
So to mix things up, you can provide a number, which is known as a "seed". That is used to initialize
the generator, and then that will cause it to generate different sequences.
Obviously, that is not terribly helpful if you are testing your code. If you make a change and the program
behaves differently,
is it because of your change, or is it because you got a different random number?
So when you are testing, it is a good idea to use the same seed every time.
And then you can change it to use a different seed when you go into production.
It is also possible to get true random numbers, but this involves interacting with the physical world.
The oldest ones are things like rolling dice or tossing coins.
In the 20th century, we discovered that physical processes produce random numbers: things like thermal
noise, from electrons rattling around in conductors. Radioactive decay.
How many particles per second?
Moving into the modern world, computers have quite a lot of internal stuff, which is random. Things
like process id, activity on the mouse or keyboard, the temperature of the CPU.
And if you mix them all up, you get something which is pretty close to being random.
And you can also get specialized hardware devices, which you can connect up to your computer.
And these will use things like air pressure fluctuations to generate random numbers, in real time.
Okay, so that is it for this video.
I will see you next time, but until then, keep coding!