WEBVTT

00:00.150 --> 00:06.860
Hello again! In this video, we are going to have a practical based upon the so-called "random walk". A

00:06.870 --> 00:08.910
random walk is in Mathematics or Physics.

00:09.270 --> 00:14.820
If we have something which is moving around, and it keeps changing its direction at random, we end up

00:14.820 --> 00:16.230
with this random walk pattern.

00:16.800 --> 00:21.510
It is also known as a "drunkard's walk", because it seems to stagger around and not really get anywhere.

00:23.040 --> 00:30.030
There is a more serious name for it as well, which is "Brownian motion". And there are many natural processes

00:30.480 --> 00:32.730
which resemble a random walk.

00:33.060 --> 00:37.980
For example, the way that insects move around when they are flying. The way that gas molecules bounce

00:37.980 --> 00:43.280
off each other. And for a long time, it was thought that financial markets follow a random walk.

00:43.290 --> 00:44.760
But in fact, that is not quite true.

00:45.270 --> 00:48.030
There are extreme movements which cannot be accounted for

00:48.030 --> 00:54.300
by randomness. In computer games, we can use the random walk to make characters move around in a more

00:54.300 --> 00:56.010
interesting and unpredictable way.

00:56.880 --> 00:57.810
So it looks like this.

00:57.810 --> 01:01.470
You can see there is a red dot bouncing around near the centre of the screen, which does not really

01:01.470 --> 01:02.520
manage to get away from it.

01:04.050 --> 01:08.520
I am not actually going to go through the code in this video. When we get to the project,

01:09.000 --> 01:13.470
I will show you how to set up a graphics library, and then we will go through this properly, along with other

01:13.470 --> 01:13.830
things.

01:15.060 --> 01:19.860
But I will give you the source code as a downloadable attachment for this video, so you can look through

01:19.860 --> 01:21.000
that, if you are interested.

01:21.720 --> 01:26.970
There are lots of comments, so if you have used a graphics library before, you can work out what is

01:26.970 --> 01:27.360
going on.

01:27.390 --> 01:30.900
I hope! This is using the SFML graphics Library.

01:32.190 --> 01:33.270
So how does this work?

01:33.600 --> 01:36.120
We have a character which is at some position on the screen.

01:36.570 --> 01:41.370
It will be "x" pixels from the left of the screen and "y" pixels from the top.

01:41.820 --> 01:47.340
So we are using the normal computer graphics convention, in which y equals 0 at the top of the screen

01:47.730 --> 01:52.620
and increases as you go downwards. As opposed to the bitmap format, which is the other way round.

01:54.210 --> 01:56.470
If we want the character to be at some other position.

01:56.490 --> 02:02.910
We need to find out how many pixels across, left or right, the character needs to move, and how many pixels

02:02.910 --> 02:05.010
up or down, to get to the new position.

02:07.350 --> 02:13.140
So we have something like this. We have the new position here. We need to move "vx" pixels, to the right

02:13.140 --> 02:16.650
in this case, and "vy" pixels down in this case.

02:17.250 --> 02:22.230
And then we can have a loop. And on each iteration, the character moves to a new position.

02:22.890 --> 02:24.150
We calculate this position.

02:24.720 --> 02:29.430
We clear the screen and then we draw the screen again, showing the character in the new position.

02:30.090 --> 02:33.750
And if we can do that quickly enough, the character will appear to move smoothly around the screen.

02:35.070 --> 02:40.440
These vx and vy have signs. If they are positive, then the character is going to move to the right,

02:40.440 --> 02:41.250
or downwards.

02:41.970 --> 02:45.240
If they are negative, the character is going to move to the left, or upwards.

02:48.140 --> 02:53.600
So to get the new position of the character, we just add vx to the x coordinate and vy to the

02:53.600 --> 02:54.350
y coordinate.

02:54.920 --> 02:59.990
If we use the same values of vx and vy all the time, with the same sign, then the character is

02:59.990 --> 03:04.370
just going to move in a straight line across the street, which is not going to be very interesting.

03:05.690 --> 03:08.490
We can make it more interesting by doing a random walk.

03:08.510 --> 03:10.070
So we need some randomness.

03:11.240 --> 03:13.310
We are going to use the Bernoulli distribution.

03:13.640 --> 03:17.060
This will convert the random numbers to either true or false.

03:17.660 --> 03:19.460
There will be a 50-50 chance of each one.

03:19.790 --> 03:23.690
And then if the random number is true, we are going to leave vx as it is.

03:23.750 --> 03:27.440
So if the character is going to the right, it is going to continue going to the right.

03:28.220 --> 03:29.960
If it is false, we invert the sign.

03:30.380 --> 03:34.940
So if it was going to the right before, it will now go to the left. And vice versa. And we do the same

03:34.940 --> 03:36.830
thing for the y velocity.

03:37.430 --> 03:43.380
So this vx and vy are just the velocity, the distance that the character moves in the horizontal

03:43.380 --> 03:45.260
or vertical direction between each update.

03:48.100 --> 03:52.750
And then we add the velocities to the x andy y, and we can re-draw the screen. And that will cause

03:52.750 --> 03:54.580
the character to follow a random walk.

03:55.600 --> 03:58.270
I have some code to this, which does not use graphics.

03:58.270 --> 04:03.280
We are going to use the console and print a row of spaces with a dot.

04:04.060 --> 04:07.930
So depending on the number of spaces, the dot will appear in a different position.

04:09.490 --> 04:15.280
We are just using one dimension, so we only have x and vx to worry about. There is no y or vy.

04:16.450 --> 04:18.460
We need to know how long this row is going to be.

04:18.460 --> 04:21.040
I am going to make it 40 because that is a nice, safe number.

04:22.720 --> 04:24.400
We need to be able to clear the screen.

04:24.400 --> 04:29.260
That means printing a row which is completely blank, so nothing but spaces.

04:29.800 --> 04:32.770
So we need to have a string which has 40 spaces in it.

04:34.720 --> 04:37.390
Then we have our random number engine and distribution.

04:38.710 --> 04:39.610
And then we have our loop.

04:39.640 --> 04:43.390
So every time through this loop, the character is going to move to a new position.

04:45.540 --> 04:47.010
Then we get our random number.

04:48.930 --> 04:53.610
If it is true, then the character is going to move to the right and if it is false, the character is

04:53.610 --> 04:54.630
going to move to the left.

04:55.140 --> 04:57.590
So this this is actually slightly different from this slide,

04:57.810 --> 04:58.650
so apologies for that.

05:00.060 --> 05:04.170
We also need to make sure that the character stays within its rightful place.

05:04.710 --> 05:09.690
So if it gets to the left-hand edge of the screen, where x equals 0, then we are going to make

05:09.690 --> 05:10.770
the velocity positive.

05:11.190 --> 05:13.530
So it is going to move to the right, away from the edge of the screen.

05:14.040 --> 05:19.380
So that will make it look as though the dot is bouncing off the edge of the screen. And at the other side,

05:19.380 --> 05:21.690
if x is equal to width at the right hand side of the screen,

05:22.140 --> 05:25.920
we are going to make the particle go to the left. So it is going to bounce off the side of the screen.

05:27.510 --> 05:31.680
Then, once we have our x velocity, we add it to the x coordinate.

05:32.910 --> 05:34.920
Then we clear the screen, by printing the blank line.

05:35.670 --> 05:40.140
This is the carriage return character. So it means that anything that appears after that is going to

05:40.140 --> 05:42.810
be on the same line, but at the start of the line.

05:43.830 --> 05:46.230
So that is going to overwrite anything that was already there.

05:48.150 --> 05:53.610
And then we make up a string which has "x" number of spaces, and then print

05:53.610 --> 05:54.720
that, followed by a dot.

05:55.080 --> 05:59.070
So if x is 6, we are going to have 6 pieces, followed by a dot.

06:00.090 --> 06:03.960
And again, we use the carriage return, to make sure this appears at the start of the line.

06:05.490 --> 06:09.120
Then the last thing we are going to do is to slow the program down a bit, so we can see what is going on.

06:09.810 --> 06:12.990
There is now a standard way to make a C++ program

06:12.990 --> 06:17.970
pause or sleep. It is actually from the threads library, but you can use it with single-threaded programs

06:17.970 --> 06:18.390
as well.

06:20.010 --> 06:25.650
This underscore thread, double colon, sleep underscore four. And then the argument is the length

06:25.650 --> 06:28.200
of time that the program is going to stop for.

06:29.400 --> 06:31.280
And that will need the <thread> header.

06:33.960 --> 06:39.180
Right. I am not actually going to run this in Visual Studio, because it flickers rather badly, so I am

06:39.180 --> 06:40.260
going to run this in MinGW.

06:42.960 --> 06:43.860
And there we are.

06:43.860 --> 06:45.750
We can see it is performing this random walk.

06:48.000 --> 06:50.700
And again, it is not really getting very far away from the left side.

06:54.120 --> 06:58.380
By default, this Bernoulli distribution gives a 50-50 chance of getting true or false.

06:58.950 --> 07:03.150
So if we call it 10 times, we expect to get 5 "true" and 5 "false".

07:04.020 --> 07:06.380
We can provide an argument to this constructor.

07:06.900 --> 07:10.110
So this will introduce a bias to the distribution.

07:10.560 --> 07:16.710
So if we put 0.8, that means we get "true" 8 times out of 10 and "false" only twice.

07:17.430 --> 07:21.810
So this should mean that the character is going to move to the right eight times and to the left only

07:21.810 --> 07:22.230
twice.

07:23.370 --> 07:29.170
So let's run that in MinGW, and we should see the character start moving.

07:29.190 --> 07:29.900
Yes, there it goes.

07:30.210 --> 07:31.230
So it is now going to the right.

07:31.620 --> 07:35.430
So 8 out of 10 times it is going to go that way, and now it's stuck at the end.

07:36.600 --> 07:38.040
Okay, so that is it for this video.

07:38.520 --> 07:39.300
I will see you next time.

07:39.510 --> 07:41.190
Until then, keep coding!
