A number of people have asked to see the code for this approximation, so here's an implementation in Python: from random import random total = 3141 count = 0 for i in range(total): x = random() y = random() if x*x + y*y
@piedepew2 жыл бұрын
You from which university bro?
@mainios126bobeats42 жыл бұрын
A one liner I made (with some code to print the result nicely): from random import random from math import pi total = 1000000 def approximate_pi(total): return 4 * sum(random() ** 2 + random() ** 2
@__lasevix_2 жыл бұрын
@@mainios126bobeats4 you should implement the function as a lambda
@mainios126bobeats42 жыл бұрын
@@__lasevix_ When I said one-liner I wasn’t referring to the whole approximate_pi, I meant the code inside. But here: from random import random approximate_pi = lambda total: 4 * sum(random() ** 2 + random() ** 2
@aronbucca67772 жыл бұрын
By the fundamental theorem of engineering pi is equal to three, it's simple
@joebykaeby2 жыл бұрын
“A bit over 3,000,” you say. That wouldn’t be 3,141 by chance?
@lyrimetacurl02 жыл бұрын
4000 would have been better since then the result should be ~ 3142/4000 (it rounds up)
@rupert_14912 жыл бұрын
You actually counted it
@flotzle36852 жыл бұрын
@@rupert_1491 No, that's what pi is lol
@silenceisgolden17582 жыл бұрын
0:44 top left shows it
@almostperfectbruh35172 жыл бұрын
bro did you just count all of them
@natedor77392 жыл бұрын
i am completely amazed at the fact that this is not extremely popular considering how clean this editing and explanation is
@nubdotdev2 жыл бұрын
Thanks a lot!
@quintium12 жыл бұрын
@@jay1373 Not the method, but the video
@dfsgjlgsdklgjnmsidrg2 жыл бұрын
thats because there is no way without a quantum computer to generate true random numbers, therefore this experiment is always biased
@direwolf272 жыл бұрын
Newton did it better with calculus and Pascal's triangle
@LymiaKanokawa2 жыл бұрын
@@dfsgjlgsdklgjnmsidrg Most modern CPUs contain a hardware RNG that uses quantum mechanical effects to generate the raw randomness. This is demonstrably false, and a lot of our current internet security relies on the fact that this is demonstrably false.
@rjms062 жыл бұрын
"A bit over 3000" I see what you've done here
@nubdotdev2 жыл бұрын
;)
@johnchristian77882 жыл бұрын
Can someone please explain the joke?
@realmarce2 жыл бұрын
@@johnchristian7788 Pi is a bit over 3 Its not that hard to understand
@maiq_68212 жыл бұрын
@@realmarce your reason for lacking bitches is also not hard to understand
@vari15352 жыл бұрын
@@johnchristian7788 They used 3141 points, which is the integer part of 1000π
@primenumberbuster4042 жыл бұрын
This is basic Monte carlo sampling.
@primenumberbuster4042 жыл бұрын
@@coreyhollaway7368 Math is suppose to be hard and nothing to feel embarassed. It keeps getting hard as you climb the ladder. Just give enough time. Also, I don't always recommend self study for Math because in most cases you get to miss a lot of interesting problem solving techniques, intuition and proper mindset. This could be avoided if you get yourself a very good mentor. Think this like a olympic mental sport. That's why Math Olympiad coaches exist.
@mrK290112 жыл бұрын
I may be a lil bit of a geek. But I still do this from time to time just for fun. Especially on Pi day even though I am a medical student now my passion for mathematics has never faded😂. Subscribed🙂🙂🙂
@vishwarao60647 ай бұрын
Woah its the opposite for me
@abdullahaddous70812 жыл бұрын
That's great. One thing I find interesting is how points are meant to be dimension less, yet we use them to approximate area.
@pigmounterboardriver28392 жыл бұрын
It's not area it's a ratio
@xGOKOPx2 жыл бұрын
@@pigmounterboardriver2839 Yes, ratio of areas.
@milaanvigraham86642 жыл бұрын
That's kind of like saying dy/dx has two dimensionless entities (both numerator and denominator) but we use it to know the rate of change/slope of a real quantity. Interesting thought you brought up, but it's quite intuitive. It's not the dimensions of the point that matters - it's the probability that it will lie within the area vs out - the point's dimensions don't matter, but these areas'
@MattMcIrvin6 ай бұрын
It's interesting to think about. The points are dimensionless, but the probability density has "per unit area" units, and that's where the area comes in.
@QwertierMannier-yp2hb4 ай бұрын
Infinite points make lines, infinite lines make area. Limiting to infinite points approximated area
@jfd70904 ай бұрын
No it is not close... The central limit theorem shows that these Monte Carlo methods are a fast but dirty and therefore approximate way of calculating multi dimensional integrals.
@Lore_play12 жыл бұрын
POV: you were trying to find some anime videos but you get this in your Recommended
@cy832 жыл бұрын
Theres actually a full lengthed video made by Joma that talks about this!
@SophieJMore2 жыл бұрын
Do points have to be randomly distributed? What if they're just arranged in a grid? Would the approximation be better this way?
@chayanaggarwal34312 жыл бұрын
Well umm you could this by just integraring on a quarter unit circle and equate it with binomial expanded form of the same integral and then you could calculate pie on any order you want (I know what he did in the video was kind of integration only )
@aarnavsood282 жыл бұрын
Title says "random way to calculate pi", not the best way. There are many many ways to approximate its value and what you're saying is exactly what Newton did.
@pafnutiytheartist2 жыл бұрын
We actually coded this for CS class a few years back. Good times
@Llamaful2 жыл бұрын
How is it random? It literally uses a circle as the main calculation.
@garrettrhoads63292 жыл бұрын
The points are placed randomly in the square
@garrettrhoads63292 жыл бұрын
I saw this and thought “huh, I could code that…” and then I did… it’s been four hours
@kienthanhle62302 жыл бұрын
you sucks at implementation then lol
@mhmd-mc1132 жыл бұрын
If you want help, I'll gladly do
@garrettrhoads63292 жыл бұрын
Nah thx tho I managed to make it with turtle and a pure math one that’s much more efficient, this also lead me to make a reimman sums pi approximation which dispute not being as cool is much more accurate and practical
@mhmd-mc1132 жыл бұрын
@@garrettrhoads6329 if you want fun ways to calculate pi, find the probability of 2 random numbers being coprime (coprime meaning with a gcd -greatest common factor- of 1)
@garrettrhoads63292 жыл бұрын
@@mhmd-mc113 wouldn’t you need a nested for loop to do that? is there a way that’s more efficient using recursion or something?
@josephpress.2 жыл бұрын
Python visualization (you need the turtle module) import sys sys.setExecutionLimit(60000) import turtle import random dart = turtle.Turtle() wn = turtle.Screen() wn.setworldcoordinates(-1,-1,1,1) dart.up() dart.tracer(200) numdarts = 10000 insideCount = 0 for i in range(numdarts): randx = random.uniform(-1, 1) randy = random.random() * 2 - 1 x = randx y = randy # Move to position dart.goto(x, y) # Colour red if within dartboard and count. Otherwise colour blue. if dart.distance(0,0)
@__________________________hi5217 күн бұрын
I know this is not meant to be the simplest way but i use ((-0.5)!)^2 which is exactly pi, the factorial of -0.5, squared
@rajubhaimarwadi95152 жыл бұрын
But you actually count those points??? 🤯
@nubdotdev2 жыл бұрын
Well, my computer did :)
@Astromath2 жыл бұрын
He wrote a script selecting a random x and y coordinate lying inside the square. Then for every point, the code checks if x²+y²
@shanewalsch2 жыл бұрын
Write a program
@viveck12792 жыл бұрын
@@nubdotdev need that program code
@prathamkalgutkar75382 жыл бұрын
@@viveck1279 Check the Pinned Comment
@revenevan112 жыл бұрын
Just found your channel and subscribed, I hope you keep making stuff like this from time to time!
@Inderastein2 жыл бұрын
Infinite balls in the circle=Area of the circle Infinite balls inside the square=Area of the square Area of the circle/Area of the square=π/4 4(area of circle/area of square)=pi 3.14159265...=Pi
@megamaz1082 жыл бұрын
Tried it out myself and averaged out at 2.9 no matter the amount of points :/ nvm I forgot decimals existed.
@Thamios2 жыл бұрын
It's more of an estimation rather a calculation
@mlembleh54512 жыл бұрын
Just in case if anyone want the code for this algorithm, it's written in javaScript so you can run the code in browser console . var n = 1000000; var ins = 0, outs = 0; for(var i = 0; i < n ;i++){ var x = Math.random(); var y = Math.random(); var z = Math.sqrt(x*x + y*y); if(z
@yuma93252 жыл бұрын
commenting for the algorithm, this is some amazing stuff!
@corbi064 ай бұрын
you can also get rid of a random: count = 0 rad = 1000 total = (rad*2)**2 for x in range(rad*2): for y in range(rad*2): if (x-rad)**2 + (y-rad)**2
@thecandybarbandit2 жыл бұрын
Imagine losing count of your dots
@Snoozy_FTW Жыл бұрын
Veritasium and physics girl did this experiment with using darts eight years ago They published video on 14 march 2015 And the result was shocking 3.139 with almost 0.1% of approximation error Also he did with drifting a car and using physics which is not dependent on area or circumference property of circle. That's cool though. Do anyone know any other method then tell
@drenz15232 жыл бұрын
How is this channel and the video not that popular already?
@cipherxen22 жыл бұрын
Denominator is already 3141
@nubdotdev2 жыл бұрын
I just chose that number because it’s pi day but you could use any sufficiently large number of points and get the same result.
@cipherxen22 жыл бұрын
@@nubdotdev Yeah. I know.
@joeylawn361112 жыл бұрын
355 ÷ 113 gives you Pi to an accuracy good for most purposes. This is ~0.000000267 greater than π.
@GWCGamingwithChittaarth2 жыл бұрын
This only works if you take a bit over 3000 and below 3143
@misteick2 жыл бұрын
Code ?
@fatitankeris63272 жыл бұрын
Remember 3Blue1Brown making a video about this.
@tofaa36682 жыл бұрын
Here is another calculation of pi. Create a perfect world where no energy is lost on collision and have 1 wall and 2 blocks 1 x times heavier than the other, count how many back and forth collisions it makes and the number should be a x amount of digits of pi
@JwalinBhatt Жыл бұрын
Does the numbers being "random" have any significance? What if we just took the points uniformly?
@darkzonegaming91694 ай бұрын
I think by random he meant uniform, I think what I means to be uniform is if the area and the number of dots in that area are directly proportional
@onelivingsoul29622 жыл бұрын
I have tried this in matlab and it took about 80,000 points to get to 4 digit accuracy
@ryantony55862 жыл бұрын
This is a programing interview question 😂😂😂
@Shad0wWarr10r2 жыл бұрын
Why would you even need to randomize the points instead of just making a mesh with every point 0.001 between eachother
@nonstopdude Жыл бұрын
i had a very wonderful pi day.. you have a wonderful pi day my "points" fella
@Bob782 жыл бұрын
Nice video, but why do they need to be in random places? Why not a uniform grid. Surely that would be even closer to pi?
@sjk74672 жыл бұрын
Shouldn't similar things work for literally any shape? Draw a square, put any shape in, see what ratio of points fall in it, now you can calculate the area.
@frankineskinecense2 жыл бұрын
This is how schools should teach maths. They have to visualize it and explain how a certain formula is made.
@alexvitkov2 жыл бұрын
i mean, you could just pick points uniformly, there's no need to introduce the RNG variance
@Well___hello Жыл бұрын
In what what world 3.1415… is very close to 3.1429… ??
@pujanmodha27492 жыл бұрын
I took just one point and i got value of pi from parallel universe
@yoursleepparalysisdemon18282 жыл бұрын
would this be better if the points were in rows, or completely random?
@ImZyron2 жыл бұрын
I know i'm like two months late but theres a video by The Coding Train on youtube about this where he describes all this and how to code it as proof
@FM12342 жыл бұрын
This was actually a challenge to approximate pi in python during my first year first semester class on programming
@panzerkampfwagentigerausfb63782 жыл бұрын
You did a great job Justin! It's always cool to learn something random here and there, I already can see myself telling this to a random friend at a party lmao.
@davidtatum86822 жыл бұрын
I was with you for like 5 seconds. Then my brain liquefied and ran out of my nose
@DerB232 жыл бұрын
Why does the distribution have to be random? Would the result be different when using a completely even distribution in a geometric pattern?
@tempname82632 жыл бұрын
No.
@Sans________________________968 ай бұрын
Draw square with 𝝅 A green dots Draw circle It’s 𝝅
@GynxShinx2 жыл бұрын
Yeah, I did this in an Excel spreadsheet when I had nothing to do at work.
@zachwaldron71462 жыл бұрын
I wish i knew what he was talking about. Understanding maths seems cool
@ababwe76572 жыл бұрын
I am not smart enough to understand this
@mayonster47982 жыл бұрын
Or hear me out, you take a circle with the diameter 1. Boom your circumference is pi
@leothesleepingdog60552 жыл бұрын
I think i went s bit overboard putting in 69420 dots in there 😳
@ilham-t9y2 жыл бұрын
Pie
@abdou4alg2 жыл бұрын
I am surprised neither the video nor the comments mention that this is called Monte Carlo method
@ripleyxo6312 жыл бұрын
Pi? Numbers? What are you talking about lets get some bananas
@catwithshades75772 жыл бұрын
I didn't see anything inside. Am I colorblind?
@JustaReadingguy2 жыл бұрын
My calculator has a better appreciation with a single button.
@UberBossPure2 жыл бұрын
Why you use x square and y square? For what it stands? I understood r square is the circle. x*x or y*y are both getting the surface of an square. But why we use it 2 times?
@tempname82632 жыл бұрын
Pythagorean theorem. Look it up.
@gaviningrave17312 жыл бұрын
Im just Justin Justin time for a random way to calculate pi
@tomasugarte1205 Жыл бұрын
if its on the line?
@darkzonegaming91694 ай бұрын
We can safely ignore them as those dots don't contribute to the area of the circle and aren't needed in the calculations, as the number of dots go to infinity the proportion of points on the line go to 0 (I think)
@elitememes72682 жыл бұрын
Did this around a year ago in computational science class tenth grade
@codyx82732 жыл бұрын
Shouldn’t it be D squared and not R squared?
@jellybeaniac1522 жыл бұрын
Yes ( I understood nothing)
@mrtank20102 жыл бұрын
me a 5th grader this makes no sience
@Ahris_aus_der_8._Dimension2 жыл бұрын
Mhh, i have tryed it with 100 mio. points several times. the best wars 3.14164.
@MrTrogdor20992 жыл бұрын
Damn that's pretty bad. Might be good to use a better random function
@tungduong51792 жыл бұрын
i can just put 1 point outside the circle and pi would equal 0
@sploofmcsterra47862 жыл бұрын
Why a random distribution over a uniform one?
@_Unknown420_2 жыл бұрын
Me: yes maths My brain: 1+1=11
@VestinVestin2 жыл бұрын
Fucking implicit type conversion...
@littlebabypuffer2 жыл бұрын
until x == pi { x = Math.Random };
@oholaoholiba6467 Жыл бұрын
is pi a mutyplier or just random summary? 😇
@mathiusr2182 жыл бұрын
JESUS loves you 🙏🏻🙏🏻🙏🏻
@thegodsofai38452 жыл бұрын
Justin cracked at math!?!?!
@alex-h3x1b2 жыл бұрын
i thought it was peas in the thumbnail XD
@cariboubearmalachy11742 жыл бұрын
3000 randomly generated points and 3000 boolean calculations to get accuracy to 2 decimal places? You only need 2 or 3 terms in a Maclauren series to get the same level of accuracy.
@keonscorner5162 жыл бұрын
*3141
@tempname82632 жыл бұрын
That's just one of the ways. No need to complain about this way even existing, lol
@jakerussell1352 жыл бұрын
if you know how to find the area of a circle, it makes perfect sense, even without watching the video and just using the thumbnail
@F1NALEXD2 жыл бұрын
He is speaking the Language of gods
@easysolutions3312 Жыл бұрын
Now do this but the dots are people.
@Siwphlhths2 жыл бұрын
should i do it on the maths finals now?
@diegovera49622 жыл бұрын
Two weeks ago, I had to do just this, but in Matlab, for a university class XDDDDD. We started with 1,000 points, then 10,000, 100,000... It was satisfying to see how "our pi" got closer and closer to the real value
@jurekis91772 жыл бұрын
THIS kind of stuff should be in my school on Pi Day
@clashoclan33712 жыл бұрын
I don't think you should give computer science topics in highschool, they probably don't even know pre-calc.
@chingchenhanji2 жыл бұрын
I remember We did this in computer science class in high school But in excel Also Joma Tech also made an explanatory video about it Wonderful, late, Pi day everyone
@Matthew_Klepadlo2 жыл бұрын
One interesting way that I like is Fracious Viete’s method: Taking 2, plus 2 over the square root of 2, plus 2 over the square root of 2 plus the square root of 2, plus 2 over the square root of 2 plus the square root of 2 plus the square root of 2, and yadda-yadda-yadda.
@fabriziobrutti12052 жыл бұрын
At the end, the famous Pi pie
@oli43ssen2 жыл бұрын
I lost you after you added the dots
@fuyalasmit2 жыл бұрын
ahh I'll better use 22/7 😌
@nexusmas92 жыл бұрын
You're cracked at fortnite
@georhodiumgeo98272 жыл бұрын
Why would you need random points? Just tile a finite grid, that way there would be no random fluctuations. The precision would still increase with the number of points. The randomness doesn’t “buy” you any extra precision, it seems like you think that’s helping but it’s hurting. You’re precision is the same but then you add the random fluctuations on top of that.
@spiderduckpig2 жыл бұрын
It's called a monte carlo simulation and it's very famous.
@georhodiumgeo98272 жыл бұрын
@@spiderduckpig Yeah I get that but this is literally the worst place for monte carlo simulation. Think about it this way, Fill a grid with dots evenly spaced and do the same thing. You would get the same answer BUT there are no random fluctuations. Typically monte carlo simulations are used where you have a multidimensional function (higher than 2 that we have here). If you have a high dimensional data set it isn't possible to simulate every value in every dimension so if you grab a distribution of random inputs you will see a good representation of "likely" outputs. There is nothing "likely" about Pi, it just is what it is. If you did a thousand points with monte carlo you would find that Pi is PROBABLY near 3.14 plus some precision errors. If however you did this on an even grid you would find that Pi IS 3.14 plus some value past your precision. You have the same precision just without the uncertainty. It's just a bad use for this tool.
@spiderduckpig2 жыл бұрын
@@georhodiumgeo9827 Yes, it's a very time-consuming and inefficient demonstration, but it is just a demonstration. Obviously the creator could just use one of the many different series that converge to pi quicker if they really wanted to quickly find pi, or one of thousands of others of better methods. But the point is that it's a very simple and easy demonstration, so this exact example shows up in a lot of introductory CS classes. Of course it wouldn't be hard to code a grid of points like you suggested, but it would be even easier to randomly place points.
@georhodiumgeo98272 жыл бұрын
@@spiderduckpig I must have misunderstood the video. I see now, it's like one of those life hack videos where somebody turns a sponge into a bad shoe. I agree, this is a horrible way to find Pi and also if you wanted to do it this way it's also a bad implementation of how to do the bad way of doing it. Thanks for the clarification, It just seemed like an unnecessary complicated way to do it badly. I guess I'm bothered that so many programmers are entering the workforce with tools they don't understand. I personally watched a coworker do some monte carlo simulation on a function that did not require it in any way. It was just a min-max problem that got nuked for no reason. The office walked around with a new buzzword for a week while I had to make sure, in the worst case, the equipment would work within parameters.
@zijunryanmeng39212 жыл бұрын
If you can draw a circle, woudn't you know pi already.
@clashoclan33712 жыл бұрын
But who can draw a circle, like a perfect one.
@deepasrip95192 жыл бұрын
Him : we have to calculate pi by counting these dots . Me : π = circumference / diamerer
@succathog59062 жыл бұрын
Lmao I thought it was grass
@Envengerx2 жыл бұрын
How would you draw a circle programmatically without knowing the value of PI?
@blobfish16492 жыл бұрын
Shhhh
@tengraint2 жыл бұрын
that's the thing, you don't. you just pretend there is one.
@MrTrogdor20992 жыл бұрын
You never draw the square, or the points, or the circle. You randomly generate their coordinates within the square (center of the square is 0,0, top-left is -R,R, top-right is R,R, bottom-left is -R,-R, bottom-right is R,-R). So your Xs and Ys will both be in the interval [-R,R].A circle centered at 0,0 and radius R is defined by points that follow x^2 + y^2 = R^2 . So your points are inside the circle if they are less than (or equal to) R away from the center, x^2 + y^2