So welcome back.
I think the plan is now clear.
We know that we can estimate PI by calculating four times the ratio of the RAAF to circle divided by
the area of the square.
And this ratio of these two areas we can determine by generating points randomly and just by counting
how many of them are inside and outside.
So let's go ahead and do this.
First of all, we must, of course, define how many points we want to have.
So I write points is equal to and let's start small.
Let's just use a thousand points.
And then I can write Rand is equal to and p dot random thoughts rand.
And then I write points.
So maybe make this number even smaller.
Let's just use ten.
And so if I now call and we have an array of 10 different numbers in the range of zero and one.
In this case, it's a bit odd because we have three of these points that are very close to one.
But if I repeat this, then you see it changes.
So really, these are randomly generated numbers from what we want to have here.
When we look at our graph is we want to have points in the range of minus one and one.
So I write two times this random number minus one.
This time we have points in the range of minus one and plus one.
Let's do it again c from minus one to plus one.
This one here is pretty close to +1.
So you see it works.
Now the thing is here we just have 10 points, but just basically in one dimension.
So just a single coordinates and we want to have X and Y coordinates.
So we will generate here a number of 20 points in our case of two times points.
So then we have 20 of them and then we will write random three shape points.
Comma to what this does is it generates pairs of these numbers.
So basically first we generate 20 points and then we will order them.
We'll just take the first to make them a list.
Second, to make them in the list.
And so now we have these pairs of these coordinates and these will be 10 of our points.
OK, so let's cut this, we don't need it anymore.
And this one we can call friend points.
OK.
And as I said previously, we need, of course, more points.
Let's start with a thousand.
So now we have hour variable rent points, 1000 points with two coordinates each, and each of these
coordinates is in the range of minus one and +1.
Now we need the norm.
So basically, we need the criterion to decide whether or not the points inside or outside the circle.
So if we take the norm, so basically the distance of the points to the center, then we know if it
is outside or inside.
Because if this length, this distance is smaller than the radius, then it must be inside.
And if it is larger and the radius, then it must be outside.
So we need to know and this will be the criterion.
So I write norm points and to calculate this, we basically take our list of red points and we take
every single one of them.
And for every single one of these points, we take the first coordinates, square it and we take the
second coordinates and square and at these two values.
So now we could right here NPR dot square root to have the actual norm.
But to be honest, it makes the code a bit slower and it's not really required, so we can just leave
it out here.
So this will now actually be the norm square points list.
But I just call it norm points.
Even though this is not the actual norm, this is the square of the norm.
However, the good thing is when the radius is one and also the square radius will be one.
So we can now just see it say if the square of the norm is smaller than one, then it's inside.
And if the square of the norm is larger than one, then it's outside.
So there's value here.
This list of values will contain valuable information to determine this ratio here.
And so what we have to do next is we have to distinguish these points based on this criterion that I
just mentioned.
So, all right, points out, is equal to random points.
So Rand's points recorded it.
So it will be this list, but only the elements which fulfil a certain criterion.
And this is pretty easy to implement in Python.
It just writes the criterion that you want to use, which in this case is also a list of the same length
and larger than one.
So what happens here is the following.
So it will start with the first entry in the round points and then it will determine based on the first
entry of this list.
So it will see OK, round points.
First element will be this one norm points.
The first element will be
what's happening.
No points.
Oh, I didn't try it.
OK, so now Norm points, the first element is this one, so the length of the of this off the distance
of this point compared to the center, Kornet squared is equal to zero point four two.
So there's is smaller than one.
So it will not be in this case, so it will not be in points out.
Instead, it will be in points in where the condition will be like this.
So it doesn't really matter if you use your smaller and equal or two smaller because due to the random
function, the chance is practically zero that there is a point where the norm is exactly equal to one.
So it doesn't really matter if we use this or this.
So like this, it should work.
And now if we look at brands, if we look at points out the first element we see again our point from
before.
No, we don't.
Of course, no, we don't, because it's not in here, but it's instead and points in.
So here we see it.
Yeah, here it is.
It's the same point.
And you see or distinction worked.
So now this point is only and points in and it's not and points out.
So no, I will delete all of these test cases here that I've just added to show you what is happening
here.
And these are the commands that we need.
Rand runs, points, norm points, points out, points in.
And to be honest, we only really need the points in.
So we could also comment this one because this is not really a requirement.
This is because we know already how many points we have, and we only need to know how many of them
are in site.
OK.
So to get the result, we are almost finished now, we can just write pipe approx.
Is equal to four times the ratio of the area circle divided by area of square.
So it would be the number of points inside divided by the total number of points.
So it would be the length of points in divided by the total number of points and to plot the or two
to output the result.
We just write print pi approx.
And you see it's three point one two four.
So we can compare this with the actual value.
And you see the difference is not that big.
So quite a good result.
Of course, we can now go ahead and just increase this number.
And the result got a bit better.
Christie's number even more, and the result got even better.
So of course, if we just repeat the same thing, then the value can change a bit because it is really
given by random.
So to show you or to visualize this a bit more, let's make a nice plot.
Let's take the plot from before, and let's change it a bit.
That's just add here these points that we have generated, and I will do this in the following way.
I would just add here to scatter plots, so I will write points in.
And we use all the points and the X coordinates, and for the Y coordinates, we ride like this.
Then I use color for the inside points to be,
I don't know, green green for positive and then the size of the points we can also specify, so make
the points a bit smaller.
So here we don't see anything does this because these patches are executed last, so they are intel
on top of the points.
And actually, we don't really need the rectangle anymore.
So here you see all the points that are inside, so you see it worked.
And then we can also add the points that are outside.
So I will remove this comment here.
So you see, we we only need the list points outside for plotting what not really to determine the value
of PI.
So, all right, points out comes out and the color will be red.
And you see like this, we have now a nice plot of all of the points.
So we have now how many have been 100000 points?
We generate 100000 points with two coordinates each and we have put it inside the square and there are
some of them are inside the circle and some of them are not inside the circle.
And just by comparing the ratio of the of the of the number of points, we have determined the value
of PI and we determined this value pretty accurately.
So this is a method that works very, very well.
It's based on lists and based on a race.
So you see, we have first generated this parade and we've reshaped it.
Then we've generated the second array with the norm than we have generated a new array where we have
just taken some of the values from the old array based on this condition.
And then we have just calculated the length.
So maybe if you are not so familiar with these arrays and all of these commands, this looks a bit difficult
to you.
So it is sometimes even easier to just use a loop methods and it would, I think, even be less lines
of code to do this.
And as we will do in the next lecture, what I can tell you already, this is such a loop method is
much, much slower, but it's sometimes easier.
And I want to show it to you anyway.