so i bought a new drawing tablets
because i've been following all these
instagram artists and i thought it would
make me a better drawer
well apparently it didn't unfortunately
it's too late to return it so i might as
well just use it to teach
interview questions which is why i'm
here
so one of my favorite interview
questions i'm kind of curious if you can
solve it
so ready okay
you have a function called i don't know
random
that generates a number from zero to one
randomly
and it's uniformly distributed
now calculate the number pi now i really
like this question because it
it sounds so absurd in the beginning
right it's almost as if as
it's almost similar to this question zoe
has
four apples zoe gives one apple to tom
and then she gives two apples to gary
now calculate the mass of the sun
anyways you can try to solve it but
uh let's go to the drawing board oh [ __ ]
sorry i think that's the wrong window um
it's a really nice looking patrick
though
i wonder who drew it so the basic idea
is to randomly draw points from a one to
one
square or like a one to one grid since
you can call the random function
twice and then what happens is that you
get two numbers
and then you use them one for the x-axis
and one for the y-axis
so for example if x becomes 0.2 and
y becomes 0.6 then what you're gonna get
is like maybe a point somewhere like
here
right so why don't we draw a few more of
these
now i'm going to give you a hint so i'm
going to draw something and i think you
will know how to do it now
so as you can see the goal here is to
count all the points in the circle
and count all the points in the square
the ratio between these two numbers will
be pretty close
to the ratio between the total area of
this circle
and the total area of this square so how
do you know if the point is in the
circle or not
well it's pretty simple you just take
the distance between the point
to the origin and if it's smaller than
one then it's in the circle
for example let's take this one all
right yeah
so that line what is the distance well
the distance is basically
x squared plus y squared square root of
that right
and then if that is smaller than one
then it's in the circle
if it's bigger than one then it's
outside the circle
but still inside the square and then now
it's just basic algebra
right so we all know that the area of a
circle
is pi r squared
oh you didn't know [ __ ] idiot
and then in this case the area of a
square
would be 2 times r and then you
square that and then with this ratio
it's going to be the same as the ratio
of
the number of points in the circle so
num
point circle divided by
the number of total points so
toto so then now what we want to do is
we just want to isolate
pi so that's pretty easy because we know
that r
equals one here so this can be gone this
becomes
two two to the two becomes four and then
we just put the four to the other side
so now it becomes
pi equals four times the number of
points in the circle
divided by the number of points total
right i don't want to write it so i'm
just gonna draw these
uh booby looking things at least it's
color coded so that's nice
and then if you want a good example so
we could actually count them
okay how many points do we have what do
you think if i i don't know maybe eight
eight points here and then how many
points do we have total like maybe two
more points here
obviously i'm not counting but you know
let's just say it's 8 divided by 10
so we get 8 divided by 10 times 4 i
guess that's pi
right and then yeah so that's pretty
much it
uh yeah coding answer should be pretty
easy so i'm not going to go over it
actually actually actually actually i
changed my mind
um i think i am going to show you the
code for it and uh
yeah because i feel like i can explain
it a little bit better while calling so
let's do that okay
all right cool so we got our online repo
right now and uh yeah let's just
start coding it so what we want to do is
call it estimate pi
and then i'm going to give it an n
meaning how many points do i actually
want to put in
so the more points the more accurate
right okay
cool so then first i want to know how
many points are in the circle
we'll initialize that to zero and then
also how many points
are in the total meaning in the square
so then next what we have to do is just
keep looping and keep adding these
points so
this means we're just looping n times
okay uh let's see is it is it this is it
right no that's not it
okay let me check something
okay excellent
okay uh
python random
okay cool random uniform got it got it
all right so first i want to give my x
a number uniform from zero to one same
thing with my y
zero to one and then we want to
calculate the distance so the distance
with
x squared plus y squared
and then yeah we're just going to keep
it at that we don't need to square root
because
what we care about is if the distance is
smaller than one
so if you square root something that's
smaller than one it's gonna be smaller
than one if you square root something
that's bigger than one it's gonna be
bigger than one
so who cares all right
circle so then if it is inside
like if it's smaller than one then that
means it's inside circle so we add one
and then no matter what we're just gonna
add it to the point to the total
got it and then in the end i think
that's pretty much it
uh remember when we did our algebra we
did the formula we saw what pi was
so there you go you just return pi and
in pi we remember that it's four
times the number of points in the circle
divided by the number of points in total
okay let's try this out excellent
all right um estimate pi
imagine if i just want 10 points in
there oh god damn it all right
random okay let's try this again
estimate pi
excellent so 3.6 pretty close i guess
the more
uh points we put into our graph
the more accurate it becomes that is not
more accurate okay maybe a thousand okay
there we go 3.17
uh pi is actually 3.14 so
okay okay yeah a little bit more
accurate let
me check what pie is actually 3.1415
okay maybe 100 000 okay
getting closer 3.14 and remember this is
random so each time you press it
or start each time you call it it's
going to give you something different
so
okay yeah so let's try another one let's
just add another zero
okay cool yeah almost 3.1415
it's probably not going to get that
accurate let's just add one more zero
i mean it's not my computer anyway so
i'm not paying for the compute cost
it is quite long
okay oh wow 3.1415 actually that's
pretty accurate yeah so there you go
that's what
uh that's what it is and that's how you
do it
so yeah all right cool and uh i hope
that was helpful
and back to the video uh yeah coatings
should be pretty easy so i'm not gonna
go over it
that's pretty much it peace