WEBVTT

00:00.150 --> 00:03.930
Hello again! In this video, we are going to look at random number algorithms.

00:05.370 --> 00:09.720
In fact, there is only one random number algorithm in the library, which is shuffle().

00:10.470 --> 00:17.850
And this will take a iterator range and put the elements into random order. So we pass it the iterator

00:17.890 --> 00:20.040
range and a random number engine.

00:20.820 --> 00:27.150
And this will do the stuff we saw before: it will use a uniform distribution to get the random numbers.

00:28.320 --> 00:34.230
This will give "perfect shuffling", so-called, which means that all the permutations of the elements are

00:34.230 --> 00:35.130
equally likely.

00:36.600 --> 00:39.240
There was also an algorithm called random_shuffle().

00:39.870 --> 00:42.260
This used rand() and it is deprecated.

00:42.360 --> 00:44.130
I think actually it has now been removed.

00:48.200 --> 00:57.950
So here is our vector. Here is our random number engine. Here is our call to shuffle(), and here is our shuffled vector.

00:59.090 --> 00:59.410
OK.

01:00.740 --> 01:04.400
There is one other thing I want to mention, which is the Bernoulli distribution.

01:04.610 --> 01:10.370
So we have the uniform distribution, which will give a range of integers or reals, which are all equally

01:10.370 --> 01:14.180
probable. With the Bernoulli distribution, we get Booleans.

01:14.900 --> 01:17.870
So all the random numbers are scaled to give

01:17.870 --> 01:19.970
either 1 or 0. True or false.

01:21.680 --> 01:22.860
And this works the same way.

01:22.880 --> 01:29.510
So we have a Bernoulli distribution object, and we pass the random number engine to it, and that will return

01:29.510 --> 01:31.790
a random number, or a random Boolean.

01:32.780 --> 01:38.510
So this is very useful if you want to make decisions which are 50-50. You know, equivalent to tossing

01:38.510 --> 01:38.930
a coin.

01:41.270 --> 01:43.910
Okay, so let's see how things are going in my kingdom, today.

01:45.680 --> 01:50.060
"Your subjects are grateful for your wise and benevolent rule". And so they should be!

01:53.410 --> 01:57.910
As this is rather a short video, I thought it was worth having a look at how the shuffle algorithm is

01:57.910 --> 01:58.510
implemented.

01:59.620 --> 02:04.720
The basic idea is that you take an elements and then pick another element at random and then swap the

02:04.720 --> 02:05.080
two.

02:05.680 --> 02:10.000
So you have a loop where you go through, and you swap each element with another one, which is chosen at

02:10.000 --> 02:10.450
random.

02:14.350 --> 02:16.090
So here is this code.

02:16.540 --> 02:18.130
Here is my attempt at implementing it.

02:20.330 --> 02:27.410
So we have our loop in which we swap the elements, so we iterate over each element and we choose

02:27.410 --> 02:28.520
another element at random.

02:28.970 --> 02:34.010
So we are going to use a random number as the index of the element that we are swapping it with.

02:35.780 --> 02:38.560
We use the uniform_int_distribution, with ints.

02:40.190 --> 02:44.180
This is going to generate an index, so we need to make sure it is within range.

02:44.630 --> 02:47.150
We do not want any out of bounds access errors.

02:48.320 --> 02:53.750
So the range we need is from 0 to size() minus one, the number of elements minus one.

02:54.410 --> 02:57.020
And then we just loop over all the elements and swap them.

03:00.010 --> 03:01.180
Okay, so there we are.

03:01.760 --> 03:07.530
Our vector is shuffeld. The real implementation is going to be a templated function, of course,

03:07.930 --> 03:13.720
and it will have some code to decide whether to use the real or integer version of uniform distribution.

03:14.530 --> 03:19.540
The mathematics might be a bit more sophisticated. But, anyway, this will give you an idea of what

03:19.540 --> 03:19.870
it does.

03:20.980 --> 03:22.600
Okay, so that is it for this video.

03:23.080 --> 03:23.950
I will see you next time.

03:24.160 --> 03:26.020
Until then, keep coding!
