So I come back, as I have just explained, we have now used all of these list or array methods to get
the value of PI, which was pretty accurate, and then we could also generate this nice plot.
But we actually don't need this plot.
We just need the value of PI and this we can get very easily by using a loop.
And I want to show you how this works.
This is the solution.
And basically, we just have to use your two counters.
So I called them I and I called them counter here.
The AI counter will loop or will be the index for to loop over the 100000 points that we are investigating.
And the counter called counter counts, the points that are inside the the circle.
So here I use a while loop.
You could also use any other loop and I write while I is smaller than points, which is 100000 in our
case, then we do something.
And of course, we also increase the value of the counter off the running index by one.
And what we do is dependent on an if statement.
So if the norm can write this, if the norm and Peter O'Toole in our thought norm.
Of a randomly generated pair of numbers is smaller than one.
Then
at the counter or increased counter by one, so we must generate a pair of two numbers.
This would be A. Dodge RAM and the daughter and two.
So this would be two numbers in the range of zero and one, but we want minus want one.
So we write like this as we did before.
And you see, now we have no information about the points.
None of this is stored, but we don't really need it.
We just need to have, in the end, the value of counter.
So I run the cell and you see, it took already a second or so and to get the value of PI approx.
All right, this is equal to four times the value of counter divided by points.
And then print pi approx.
So you see, again, pretty close to pie, and we can, of course, also calculate the difference and
p dot minus pie.
And you see, it's pretty good.
So qualitatively, it's the same result as before.
Of course, the value is now a bit different, but this is just due to randomness.
If we would repeat this many, many times and an average and the result would get even better.
And you see, this one was just a few lines of code, and it looks pretty simple.
I think it was very intuitive.
We just had to loop here using a while loop and then just use a single if statement.
And basically, you wouldn't have even been required to use such a command line.
Argument could have also wrote two times and p rand squared plus another and p random rand square and
then smaller than one.
This would also be totally fine.
So you see, it's pretty simple.
But what's the downside of this?
The downside is the speed, and I want to show you now a speed comparison of the two methods.
So you see here I have copied the two methods on how we can calculate the approximate value of PI based
on the aerialist method and based on the loop methods.
So for the loop method, I have just copied what we have written here and then just a single line of
code.
And for the RNA or at least method, I have basically copied these lines here and then only the ones
that really need it.
So this would be this one and this one.
But of course, none of this plots because this was just optional.
And of course, this takes more time, but it's not required.
So we have here just a few of these commands where we will modify our array and then he or we distinguished
and determined only the points that are inside the circle.
And you see, when we run both of these, we get no outputs because, yeah, we didn't call for an output.
But I want to add now a command that is called percent percent time.
So that's a command that you can always use in Jupyter Notebook to output the time it took to run these
cells.
When I run this year, the first time you see it takes a bit.
This is because it has run seven runs of 100 loops, so this whole cell has been calculated 700 times.
And then the total time has been divided, of course, by 700.
And you see the time for a single loop or a single run of the cell took three point twenty five milliseconds.
And now if I do this with a loop methods.
We get the time for the new method, and you see it's much, much higher.
It's 522 milliseconds and you see, since this took more than 100 times longer, it's only that seven
runs with one loop each, the only seven runs in total, compared to 700 here.
So it's a pretty cool command that allows you to really find out if your code is fast or not.
And I think this is a cool example because you see the loop method is probably the more intuitive one,
the most simple one, the one that beginners will probably choose and would have come up with.
However, it's so much slower.
In this case, it doesn't really matter because it only takes half a second anyway.
But if you would have to run this algorithm a thousand times, then it's of course much better to use
this one because it only takes three seconds here and here it takes 500 seconds, which is almost 10
minutes.
So time matters sometimes and often.
It's very, very helpful to think in advance if your code is required to be fast or not.
If it's not required to be fast, then you can choose the more simple solution sometimes and get away
with it.
But if time really methods, if it's really something that you call very, very often, then it makes
sense to think about finding a fast solution.
And that's the take-home message here, working with arrays and using these functions that correspond
to arrays like this reshape or something like this one here and also this length command.
They are much, much faster than these loops, which are not so, well optimized.
So if you want to have fast code and you should always tend to solutions like these.
However, in general, the take-home message of this whole lecture is that we have determined the value
of PI, which is generating 100000 points, and we have just counted how many of them are inside.
And then when we divide this number by 100000, we have approximated the ratio of the area of the circle
compared to the area of the square.
And in the very beginning, we have derived that the value of PI can be approximated by this ratio multiplied
by four.
I think that's really impressive example if you have never heard of it and it really shows you how you
can use randomness and Monte Carlo algorithms to find out useful information.