Hello again! In this video, we are going to have a practical based upon the so-called "random walk". A
random walk is in Mathematics or Physics.
If we have something which is moving around, and it keeps changing its direction at random, we end up
with this random walk pattern.
It is also known as a "drunkard's walk", because it seems to stagger around and not really get anywhere.
There is a more serious name for it as well, which is "Brownian motion". And there are many natural processes
which resemble a random walk.
For example, the way that insects move around when they are flying. The way that gas molecules bounce
off each other. And for a long time, it was thought that financial markets follow a random walk.
But in fact, that is not quite true.
There are extreme movements which cannot be accounted for
by randomness. In computer games, we can use the random walk to make characters move around in a more
interesting and unpredictable way.
So it looks like this.
You can see there is a red dot bouncing around near the centre of the screen, which does not really
manage to get away from it.
I am not actually going to go through the code in this video. When we get to the project,
I will show you how to set up a graphics library, and then we will go through this properly, along with other
things.
But I will give you the source code as a downloadable attachment for this video, so you can look through
that, if you are interested.
There are lots of comments, so if you have used a graphics library before, you can work out what is
going on.
I hope! This is using the SFML graphics Library.
So how does this work?
We have a character which is at some position on the screen.
It will be "x" pixels from the left of the screen and "y" pixels from the top.
So we are using the normal computer graphics convention, in which y equals 0 at the top of the screen
and increases as you go downwards. As opposed to the bitmap format, which is the other way round.
If we want the character to be at some other position.
We need to find out how many pixels across, left or right, the character needs to move, and how many pixels
up or down, to get to the new position.
So we have something like this. We have the new position here. We need to move "vx" pixels, to the right
in this case, and "vy" pixels down in this case.
And then we can have a loop. And on each iteration, the character moves to a new position.
We calculate this position.
We clear the screen and then we draw the screen again, showing the character in the new position.
And if we can do that quickly enough, the character will appear to move smoothly around the screen.
These vx and vy have signs. If they are positive, then the character is going to move to the right,
or downwards.
If they are negative, the character is going to move to the left, or upwards.
So to get the new position of the character, we just add vx to the x coordinate and vy to the
y coordinate.
If we use the same values of vx and vy all the time, with the same sign, then the character is
just going to move in a straight line across the street, which is not going to be very interesting.
We can make it more interesting by doing a random walk.
So we need some randomness.
We are going to use the Bernoulli distribution.
This will convert the random numbers to either true or false.
There will be a 50-50 chance of each one.
And then if the random number is true, we are going to leave vx as it is.
So if the character is going to the right, it is going to continue going to the right.
If it is false, we invert the sign.
So if it was going to the right before, it will now go to the left. And vice versa. And we do the same
thing for the y velocity.
So this vx and vy are just the velocity, the distance that the character moves in the horizontal
or vertical direction between each update.
And then we add the velocities to the x andy y, and we can re-draw the screen. And that will cause
the character to follow a random walk.
I have some code to this, which does not use graphics.
We are going to use the console and print a row of spaces with a dot.
So depending on the number of spaces, the dot will appear in a different position.
We are just using one dimension, so we only have x and vx to worry about. There is no y or vy.
We need to know how long this row is going to be.
I am going to make it 40 because that is a nice, safe number.
We need to be able to clear the screen.
That means printing a row which is completely blank, so nothing but spaces.
So we need to have a string which has 40 spaces in it.
Then we have our random number engine and distribution.
And then we have our loop.
So every time through this loop, the character is going to move to a new position.
Then we get our random number.
If it is true, then the character is going to move to the right and if it is false, the character is
going to move to the left.
So this this is actually slightly different from this slide,
so apologies for that.
We also need to make sure that the character stays within its rightful place.
So if it gets to the left-hand edge of the screen, where x equals 0, then we are going to make
the velocity positive.
So it is going to move to the right, away from the edge of the screen.
So that will make it look as though the dot is bouncing off the edge of the screen. And at the other side,
if x is equal to width at the right hand side of the screen,
we are going to make the particle go to the left. So it is going to bounce off the side of the screen.
Then, once we have our x velocity, we add it to the x coordinate.
Then we clear the screen, by printing the blank line.
This is the carriage return character. So it means that anything that appears after that is going to
be on the same line, but at the start of the line.
So that is going to overwrite anything that was already there.
And then we make up a string which has "x" number of spaces, and then print
that, followed by a dot.
So if x is 6, we are going to have 6 pieces, followed by a dot.
And again, we use the carriage return, to make sure this appears at the start of the line.
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.
There is now a standard way to make a C++ program
pause or sleep. It is actually from the threads library, but you can use it with single-threaded programs
as well.
This underscore thread, double colon, sleep underscore four. And then the argument is the length
of time that the program is going to stop for.
And that will need the header.
Right. I am not actually going to run this in Visual Studio, because it flickers rather badly, so I am
going to run this in MinGW.
And there we are.
We can see it is performing this random walk.
And again, it is not really getting very far away from the left side.
By default, this Bernoulli distribution gives a 50-50 chance of getting true or false.
So if we call it 10 times, we expect to get 5 "true" and 5 "false".
We can provide an argument to this constructor.
So this will introduce a bias to the distribution.
So if we put 0.8, that means we get "true" 8 times out of 10 and "false" only twice.
So this should mean that the character is going to move to the right eight times and to the left only
twice.
So let's run that in MinGW, and we should see the character start moving.
Yes, there it goes.
So it is now going to the right.
So 8 out of 10 times it is going to go that way, and now it's stuck at the end.
Okay, so that is it for this video.
I will see you next time.
Until then, keep coding!