A Random Way to Calculate Pi

  Рет қаралды 434,265

nubDotDev

nubDotDev

2 жыл бұрын

Happy Pi Day!
Animated with:
The Manim Community Developers. (2021). Manim - Mathematical Animation Framework (Version v0.12.0) [Computer software]. www.manim.community/

Пікірлер: 277
@nubdotdev
@nubdotdev 2 жыл бұрын
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
@piedepew
@piedepew 2 жыл бұрын
You from which university bro?
@mainios126bobeats4
@mainios126bobeats4 2 жыл бұрын
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_
@__lasevix_ 2 жыл бұрын
@@mainios126bobeats4 you should implement the function as a lambda
@mainios126bobeats4
@mainios126bobeats4 2 жыл бұрын
@@__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
@aronbucca6777
@aronbucca6777 2 жыл бұрын
By the fundamental theorem of engineering pi is equal to three, it's simple
@natedor7739
@natedor7739 2 жыл бұрын
i am completely amazed at the fact that this is not extremely popular considering how clean this editing and explanation is
@nubdotdev
@nubdotdev 2 жыл бұрын
Thanks a lot!
@quintium1
@quintium1 2 жыл бұрын
@@jay1373 Not the method, but the video
@dfsgjlgsdklgjnmsidrg
@dfsgjlgsdklgjnmsidrg 2 жыл бұрын
thats because there is no way without a quantum computer to generate true random numbers, therefore this experiment is always biased
@direwolf27
@direwolf27 2 жыл бұрын
Newton did it better with calculus and Pascal's triangle
@LymiaKanokawa
@LymiaKanokawa 2 жыл бұрын
@@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.
@joebykaeby
@joebykaeby 2 жыл бұрын
“A bit over 3,000,” you say. That wouldn’t be 3,141 by chance?
@lyrimetacurl0
@lyrimetacurl0 2 жыл бұрын
4000 would have been better since then the result should be ~ 3142/4000 (it rounds up)
@rupert_1491
@rupert_1491 2 жыл бұрын
You actually counted it
@flotzle3685
@flotzle3685 2 жыл бұрын
@@rupert_1491 No, that's what pi is lol
@silenceisgolden1758
@silenceisgolden1758 2 жыл бұрын
0:44 top left shows it
@almostperfectbruh3517
@almostperfectbruh3517 2 жыл бұрын
bro did you just count all of them
@rjms06
@rjms06 2 жыл бұрын
"A bit over 3000" I see what you've done here
@nubdotdev
@nubdotdev 2 жыл бұрын
;)
@johnchristian7788
@johnchristian7788 2 жыл бұрын
Can someone please explain the joke?
@realmarce
@realmarce 2 жыл бұрын
@@johnchristian7788 Pi is a bit over 3 Its not that hard to understand
@maiq_6821
@maiq_6821 2 жыл бұрын
@@realmarce your reason for lacking bitches is also not hard to understand
@vari1535
@vari1535 2 жыл бұрын
@@johnchristian7788 They used 3141 points, which is the integer part of 1000π
@abdullahaddous7081
@abdullahaddous7081 2 жыл бұрын
That's great. One thing I find interesting is how points are meant to be dimension less, yet we use them to approximate area.
@pigmounterboardriver2839
@pigmounterboardriver2839 2 жыл бұрын
It's not area it's a ratio
@xGOKOPx
@xGOKOPx 2 жыл бұрын
@@pigmounterboardriver2839 Yes, ratio of areas.
@milaanvigraham8664
@milaanvigraham8664 2 жыл бұрын
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'
@MattMcIrvin
@MattMcIrvin 3 ай бұрын
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-yp2hb
@QwertierMannier-yp2hb Ай бұрын
Infinite points make lines, infinite lines make area. Limiting to infinite points approximated area
@mrK29011
@mrK29011 2 жыл бұрын
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🙂🙂🙂
@vishwarao6064
@vishwarao6064 4 ай бұрын
Woah its the opposite for me
@primenumberbuster404
@primenumberbuster404 2 жыл бұрын
This is basic Monte carlo sampling.
@primenumberbuster404
@primenumberbuster404 2 жыл бұрын
@@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.
@panzerkampfwagentigerausfb6378
@panzerkampfwagentigerausfb6378 2 жыл бұрын
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.
@playify5538
@playify5538 2 жыл бұрын
POV: you were trying to find some anime videos but you get this in your Recommended
@pafnutiytheartist
@pafnutiytheartist 2 жыл бұрын
We actually coded this for CS class a few years back. Good times
@drenz1523
@drenz1523 2 жыл бұрын
How is this channel and the video not that popular already?
@revenevan11
@revenevan11 2 жыл бұрын
Just found your channel and subscribed, I hope you keep making stuff like this from time to time!
@yuma9325
@yuma9325 2 жыл бұрын
commenting for the algorithm, this is some amazing stuff!
@frankineskinecense
@frankineskinecense 2 жыл бұрын
This is how schools should teach maths. They have to visualize it and explain how a certain formula is made.
@randomguyeverywhere6535
@randomguyeverywhere6535 2 жыл бұрын
Props to those people who actually counted the dots.
@clashoclan3371
@clashoclan3371 2 жыл бұрын
The computers counts it.
@janni4541
@janni4541 2 жыл бұрын
It would be also very interesting to see this method compared to a method using evenly spaced dots and comparing them! This would also raise the question at what number of points one is more exact than the other.
@FM1234
@FM1234 2 жыл бұрын
This was actually a challenge to approximate pi in python during my first year first semester class on programming
@thecandybarbandit6887
@thecandybarbandit6887 2 жыл бұрын
Imagine losing count of your dots
@corbi06
@corbi06 Ай бұрын
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
@garrettrhoads6329
@garrettrhoads6329 2 жыл бұрын
I saw this and thought “huh, I could code that…” and then I did… it’s been four hours
@kienthanhle6230
@kienthanhle6230 2 жыл бұрын
you sucks at implementation then lol
@mhmd-mc113
@mhmd-mc113 2 жыл бұрын
If you want help, I'll gladly do
@garrettrhoads6329
@garrettrhoads6329 2 жыл бұрын
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-mc113
@mhmd-mc113 2 жыл бұрын
@@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)
@garrettrhoads6329
@garrettrhoads6329 2 жыл бұрын
@@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?
@chayanaggarwal3431
@chayanaggarwal3431 2 жыл бұрын
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 )
@aarnavsood28
@aarnavsood28 2 жыл бұрын
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.
@davidtatum8682
@davidtatum8682 2 жыл бұрын
I was with you for like 5 seconds. Then my brain liquefied and ran out of my nose
@jurekis9177
@jurekis9177 2 жыл бұрын
THIS kind of stuff should be in my school on Pi Day
@clashoclan3371
@clashoclan3371 2 жыл бұрын
I don't think you should give computer science topics in highschool, they probably don't even know pre-calc.
@GynxShinx
@GynxShinx 2 жыл бұрын
Yeah, I did this in an Excel spreadsheet when I had nothing to do at work.
@zachwaldron7146
@zachwaldron7146 2 жыл бұрын
I wish i knew what he was talking about. Understanding maths seems cool
@danielttk8682
@danielttk8682 2 жыл бұрын
I'm subscribed, I need more math in my life
@gregfeneis609
@gregfeneis609 2 жыл бұрын
Totally appropriate that the Pi-thagoras' theorem is used for this.
@cy83
@cy83 2 жыл бұрын
Theres actually a full lengthed video made by Joma that talks about this!
@SophieJMore
@SophieJMore 2 жыл бұрын
Do points have to be randomly distributed? What if they're just arranged in a grid? Would the approximation be better this way?
@thegodsofai3845
@thegodsofai3845 2 жыл бұрын
Justin cracked at math!?!?!
@gangstaflower2262
@gangstaflower2262 2 жыл бұрын
Amazing Video !
@gamechip06
@gamechip06 2 жыл бұрын
That's really cool
@clxse1
@clxse1 2 жыл бұрын
Yes , I totally agree with you.
@diegovera4962
@diegovera4962 2 жыл бұрын
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
@nonstopdude1211
@nonstopdude1211 Жыл бұрын
i had a very wonderful pi day.. you have a wonderful pi day my "points" fella
@rajubhaimarwadi9515
@rajubhaimarwadi9515 2 жыл бұрын
But you actually count those points??? 🤯
@nubdotdev
@nubdotdev 2 жыл бұрын
Well, my computer did :)
@Astromath
@Astromath 2 жыл бұрын
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²
@shanewalsch
@shanewalsch 2 жыл бұрын
Write a program
@viveck1279
@viveck1279 2 жыл бұрын
@@nubdotdev need that program code
@prathamkalgutkar7538
@prathamkalgutkar7538 2 жыл бұрын
@@viveck1279 Check the Pinned Comment
@Matthew_Klepadlo
@Matthew_Klepadlo 2 жыл бұрын
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.
@JustaReadingguy
@JustaReadingguy 2 жыл бұрын
My calculator has a better appreciation with a single button.
@misamisamisamisa
@misamisamisamisa 2 жыл бұрын
Cool. I figured out it won't be accurate from the start. It's too random but it's pretty close.
@Thamios
@Thamios 2 жыл бұрын
It's more of an estimation rather a calculation
@chingchenhanji
@chingchenhanji 2 жыл бұрын
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
@ryantony5586
@ryantony5586 2 жыл бұрын
This is a programing interview question 😂😂😂
@_Unknown420_
@_Unknown420_ 2 жыл бұрын
Me: yes maths My brain: 1+1=11
@VestinVestin
@VestinVestin 2 жыл бұрын
Fucking implicit type conversion...
@fabriziobrutti1205
@fabriziobrutti1205 2 жыл бұрын
At the end, the famous Pi pie
@riceisbetterthanyou5077
@riceisbetterthanyou5077 2 жыл бұрын
wow! this is very impressive
@Doctor_Drew
@Doctor_Drew 2 жыл бұрын
great video!
@maevesilvestrys5600
@maevesilvestrys5600 2 жыл бұрын
Love ❤ the video
@jfd7090
@jfd7090 Ай бұрын
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.
@ripleyxo631
@ripleyxo631 2 жыл бұрын
Pi? Numbers? What are you talking about lets get some bananas
@henrique7149
@henrique7149 2 жыл бұрын
wow, thats so cool
@fatitankeris6327
@fatitankeris6327 2 жыл бұрын
Remember 3Blue1Brown making a video about this.
@cipherxen2
@cipherxen2 2 жыл бұрын
Denominator is already 3141
@nubdotdev
@nubdotdev 2 жыл бұрын
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.
@cipherxen2
@cipherxen2 2 жыл бұрын
@@nubdotdev Yeah. I know.
@alexp15-
@alexp15- 2 жыл бұрын
i thought it was peas in the thumbnail XD
@akankshasharma7498
@akankshasharma7498 2 жыл бұрын
I did study this in my Design and Analysis of Algorithms class
@SophiePastelz
@SophiePastelz 2 жыл бұрын
Very cool,
@Loading-lg6hs
@Loading-lg6hs 2 жыл бұрын
awesome vid
@Loading-lg6hs
@Loading-lg6hs 2 жыл бұрын
5.62
@abdou4alg
@abdou4alg 2 жыл бұрын
I am surprised neither the video nor the comments mention that this is called Monte Carlo method
@tofaa3668
@tofaa3668 2 жыл бұрын
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
@pujanmodha2749
@pujanmodha2749 2 жыл бұрын
I took just one point and i got value of pi from parallel universe
@mlembleh5451
@mlembleh5451 2 жыл бұрын
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
@Siwphlhths
@Siwphlhths 2 жыл бұрын
should i do it on the maths finals now?
@alexvitkov
@alexvitkov 2 жыл бұрын
i mean, you could just pick points uniformly, there's no need to introduce the RNG variance
@ImZyron
@ImZyron 2 жыл бұрын
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
@yoursleepparalysisdemon1828
@yoursleepparalysisdemon1828 2 жыл бұрын
would this be better if the points were in rows, or completely random?
@terrsus7676
@terrsus7676 2 жыл бұрын
I think its funny that i knew what the method would be by looking at the thumbnail 😁
@kienthanhle6230
@kienthanhle6230 2 жыл бұрын
Can I make a video about testing the accuracy of this algorithms, as well as doing some optimization on it? (I will leave the credit)
@nubdotdev
@nubdotdev 2 жыл бұрын
Yeah, of course! That should be pretty interesting.
@kienthanhle6230
@kienthanhle6230 2 жыл бұрын
@@nubdotdev Thank you so much!
@lerkabishhhit
@lerkabishhhit 2 жыл бұрын
This method isn't new, it calls Monte Carlo method, and it's accuracy is pretty bad, because method error is random value, but amounts of implantations are interesting
@kienthanhle6230
@kienthanhle6230 2 жыл бұрын
@@lerkabishhhit yeah, I've tested it with the sample size of 10^9 and the output was an astonishing 4 correct digits. Like did I do anything wrong?
@Llamaful
@Llamaful 2 жыл бұрын
How is it random? It literally uses a circle as the main calculation.
@garrettrhoads6329
@garrettrhoads6329 2 жыл бұрын
The points are placed randomly in the square
@omarel-nemr6506
@omarel-nemr6506 2 жыл бұрын
Cool
@oli43ssen
@oli43ssen 2 жыл бұрын
I lost you after you added the dots
@jontedeakin1986
@jontedeakin1986 2 жыл бұрын
3blue1brown influence
@Bob78
@Bob78 2 жыл бұрын
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?
@fuyalasmit
@fuyalasmit 2 жыл бұрын
ahh I'll better use 22/7 😌
@onelivingsoul2962
@onelivingsoul2962 2 жыл бұрын
I have tried this in matlab and it took about 80,000 points to get to 4 digit accuracy
@atharvapatil6003
@atharvapatil6003 Жыл бұрын
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
@Sasoon2006
@Sasoon2006 2 жыл бұрын
This depends on how good is "random" method implementation.
@jeevankahlon5570
@jeevankahlon5570 2 жыл бұрын
i love pi
@F1NALEXD
@F1NALEXD 2 жыл бұрын
He is speaking the Language of gods
@deepasrip9519
@deepasrip9519 2 жыл бұрын
Him : we have to calculate pi by counting these dots . Me : π = circumference / diamerer
@Exisist5151
@Exisist5151 2 жыл бұрын
Aka my CS assignment last week
@oldboyiscool4040
@oldboyiscool4040 2 жыл бұрын
Why is pi so ducking versatile, plop you there and put you there and dat shiz works for some reason
@JwalinBhatt
@JwalinBhatt 10 ай бұрын
Does the numbers being "random" have any significance? What if we just took the points uniformly?
@darkzonegaming9169
@darkzonegaming9169 Ай бұрын
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
@itsroman2112
@itsroman2112 2 жыл бұрын
Crazy
@goofybidoofie
@goofybidoofie 2 жыл бұрын
monte carlo moment
@DerB23
@DerB23 2 жыл бұрын
Why does the distribution have to be random? Would the result be different when using a completely even distribution in a geometric pattern?
@tempname8263
@tempname8263 2 жыл бұрын
No.
@yes-mu4gr
@yes-mu4gr 2 жыл бұрын
cool
@Dark_Matter2
@Dark_Matter2 7 ай бұрын
I see doggo in points
@--2
@--2 2 жыл бұрын
Now we need a proof ;)
@leothesleepingdog6055
@leothesleepingdog6055 2 жыл бұрын
I think i went s bit overboard putting in 69420 dots in there 😳
@chickenmonger123
@chickenmonger123 2 жыл бұрын
What stops the “randomization” from being derived from pi in the first place? Which is easily resolved in reverse to derive pi?
@spiderduckpig
@spiderduckpig 2 жыл бұрын
The randomization is not derived by pi. You can check if a point is in the circle by just seeing if x^2 + y^2 < 1 (if the radius of the circle is 1, and the center of the circle is (0, 0)).
@Shad0wWarr10r
@Shad0wWarr10r 2 жыл бұрын
Why would you even need to randomize the points instead of just making a mesh with every point 0.001 between eachother
@easysolutions3312
@easysolutions3312 Жыл бұрын
Now do this but the dots are people.
@elitememes7268
@elitememes7268 2 жыл бұрын
Did this around a year ago in computational science class tenth grade
@megamaz108
@megamaz108 2 жыл бұрын
Tried it out myself and averaged out at 2.9 no matter the amount of points :/ nvm I forgot decimals existed.
@BruH-xg5eo
@BruH-xg5eo 2 жыл бұрын
Pi pie...
@gaviningrave1731
@gaviningrave1731 2 жыл бұрын
Im just Justin Justin time for a random way to calculate pi
@sjk7467
@sjk7467 2 жыл бұрын
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.
@succathog5906
@succathog5906 2 жыл бұрын
Lmao I thought it was grass
@ilham-t9y
@ilham-t9y 2 жыл бұрын
Pie
@julianbrudka9821
@julianbrudka9821 2 жыл бұрын
lol my brother just made it as college project
@UberBossPure
@UberBossPure 2 жыл бұрын
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?
@tempname8263
@tempname8263 2 жыл бұрын
Pythagorean theorem. Look it up.
The 360-Page Proof That 1+1=2
6:03
Half as Interesting
Рет қаралды 1,6 МЛН
Building a Relay-Powered Pi Machine
17:35
The Science Elf
Рет қаралды 281 М.
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 34 МЛН
I’m just a kid 🥹🥰 LeoNata family #shorts
00:12
LeoNata Family
Рет қаралды 19 МЛН
Khóa ly biệt
01:00
Đào Nguyễn Ánh - Hữu Hưng
Рет қаралды 21 МЛН
The Pi Song 2.0 (Memorize 200 Digits Of π)
3:15
AsapSCIENCE
Рет қаралды 2,5 МЛН
Unveiling The Remarkable Discovery Of Pi By A Genius - Prepare To Be Amazed!
9:01
Giving Personality to Procedural Animations using Math
15:30
t3ssel8r
Рет қаралды 2,4 МЛН
Best Hydraulic Press Compilation: Satisfying Crushes & Surprising Results
13:44
Hydraulic Press Channel
Рет қаралды 57 М.
1 ^ ∞, It's Not What You Think
4:28
BriTheMathGuy
Рет қаралды 941 М.
Radial Gradients Are So Powerful!
4:25
unknown1050
Рет қаралды 4,3 М.
I Made a Neural Network with just Redstone!
17:23
mattbatwings
Рет қаралды 681 М.
This Video Will Make You Better At Math
4:52
BriTheMathGuy
Рет қаралды 3,6 МЛН
Just some cool fractals | #SoME3 #shorts
0:59
nubDotDev
Рет қаралды 3,2 М.
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 34 МЛН