TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Using the Monte-Carlo simulation to estimate the value of 𝛑

2 pointsby mhasbiniover 4 years ago

1 comment

ryan-duveover 4 years ago
This is my favorite example of the Monte Carlo method. I first learned it as a farmer needs to estimate the size of a circular pond surrounding by a square fence he can&#x27;t see over, and he&#x27;s given an infinite supply of pebbles. It&#x27;s a wonderful learning tool. One question that comes up involves the finite precision of the variables you&#x27;re using. In this post, it would be this part:<p><pre><code> (x - 0.5)**2 + (y - 0.5)**2 &lt;= 0.25 </code></pre> In C, it&#x27;s easy to state the limit on how precise the final estimate for pi can be because you have to state the type of the variable you&#x27;re using to store it, and you know have many bits that type gets allocated. In Python, this doesn&#x27;t seem so simple (<a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;tutorial&#x2F;floatingpoint.html#representation-error" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;tutorial&#x2F;floatingpoint.html#repres...</a>).<p>I question whether Python knows internally to use a heap to keep scaling how many digits can fit in the pi estimate as the Monte Carlo goes on, or if there&#x27;s a limit to existing pre-defined sizes that caps out the precision. Also, it isn&#x27;t readily obvious to me how to parameterize the np.random call to keep getting more and more digits in the values for x and y so the above calculation can keep getting more precise. It&#x27;d be cool to see a Python implementation that really can scale until an arbitrary amount of available memory is used, and see the relationship of memory used to pi digits calculated.
评论 #26214732 未加载