WEBVTT

00:00.120 --> 00:06.420
Hello again! In this video, we are going to start looking at random numbers. A random number is one

00:06.420 --> 00:08.670
which appears as though it has been chosen by chance.

00:09.480 --> 00:14.850
Usually we have a range of numbers and we assume that any number in this range is equally likely to

00:14.850 --> 00:15.420
be chosen.

00:16.140 --> 00:21.990
So for example, if you have a pack of cards - playing cards - and you choose one at random, it is equally

00:21.990 --> 00:24.570
likely to be any one of the 52 possibilities.

00:25.770 --> 00:28.890
And that is what mathematicians call a "uniform distribution".

00:30.270 --> 00:35.160
We also assume that the next number we choose is not affected by the last number we chose.

00:35.700 --> 00:37.980
So the choices are "independent".

00:38.670 --> 00:40.230
So that is like tossing a coin.

00:40.230 --> 00:45.210
If you toss a coin and get heads, then you still have a 50-50 chance of getting heads or tails

00:45.210 --> 00:45.810
the next time.

00:49.530 --> 00:52.020
Random numbers are actually very useful in computing.

00:52.410 --> 00:58.710
The obvious example is in games. For example, if you have a card game, then you can deal the player

00:58.710 --> 01:00.120
a different hand every time.

01:01.050 --> 01:05.580
If you have an action game, then you can make the opponent move around unpredictably, which makes

01:05.580 --> 01:06.420
him harder to catch.

01:07.080 --> 01:11.280
If you have a game with a story, then you can choose different branches through the story.

01:11.640 --> 01:15.780
So random numbers will make games more interesting and unpredictable.

01:17.640 --> 01:23.280
Random numbers are also used in numerical computation. For example, in "Monte Carlo" simulations.

01:23.790 --> 01:27.630
If you do not have complete data, you can put in random numbers with sensible values.

01:28.050 --> 01:32.850
Then run the simulation lots of times, and take an average. And that should give you some idea of what is

01:32.850 --> 01:33.420
going to happen.

01:34.140 --> 01:36.060
So that is basically how weather forecasting is done,

01:36.060 --> 01:43.470
for example. In cryptography, we can use random numbers to create codes and ciphers, which are unbreakable.

01:43.470 --> 01:45.240
Or very hard to break, anyway!

01:46.020 --> 01:50.100
And they are also used in authentication for network security and crypto.

01:53.190 --> 01:58.530
How do we get random numbers? If we are using software, we have a bit of a problem, because software is

01:58.530 --> 02:01.380
deterministic, so it cannot do things by chance.

02:02.460 --> 02:05.820
However, there are things called "pseudo-random number generators".

02:06.540 --> 02:12.360
These are algorithms which will do some calculations, and that will generate a sequence of numbers which

02:12.360 --> 02:14.460
look fairly close to being random.

02:16.140 --> 02:18.900
Obviously, it will produce the same results every time.

02:20.160 --> 02:25.410
So to mix things up, you can provide a number, which is known as a "seed". That is used to initialize

02:25.410 --> 02:29.250
the generator, and then that will cause it to generate different sequences.

02:30.870 --> 02:35.760
Obviously, that is not terribly helpful if you are testing your code. If you make a change and the program

02:35.760 --> 02:36.630
behaves differently,

02:36.930 --> 02:40.560
is it because of your change, or is it because you got a different random number?

02:41.220 --> 02:44.070
So when you are testing, it is a good idea to use the same seed every time.

02:45.060 --> 02:47.760
And then you can change it to use a different seed when you go into production.

02:50.150 --> 02:54.650
It is also possible to get true random numbers, but this involves interacting with the physical world.

02:55.970 --> 02:59.420
The oldest ones are things like rolling dice or tossing coins.

03:00.380 --> 03:06.470
In the 20th century, we discovered that physical processes produce random numbers: things like thermal

03:06.590 --> 03:12.260
noise, from electrons rattling around in conductors. Radioactive decay.

03:12.320 --> 03:13.790
How many particles per second?

03:16.480 --> 03:21.550
Moving into the modern world, computers have quite a lot of internal stuff, which is random. Things

03:21.550 --> 03:26.260
like process id, activity on the mouse or keyboard, the temperature of the CPU.

03:26.530 --> 03:30.100
And if you mix them all up, you get something which is pretty close to being random.

03:31.750 --> 03:36.310
And you can also get specialized hardware devices, which you can connect up to your computer.

03:36.970 --> 03:42.850
And these will use things like air pressure fluctuations to generate random numbers, in real time.

03:43.780 --> 03:45.430
Okay, so that is it for this video.

03:45.850 --> 03:48.700
I will see you next time, but until then, keep coding!
