问题
Consider that following program.
import math
import random
def inside_unit_circle(point):
"""
Compute distance of point from origin
"""
distance = math.sqrt(point[0] ** 2 + point[1] ** 2)
return distance < 1
def estimate_mystery(num_trials):
"""
Main function
"""
num_inside = 0
for dumm_idx in range(num_trials):
new_point = [2 * random.random() - 1, 2 * random.random() - 1]
if inside_unit_circle(new_point):
num_inside += 1
return float(num_inside) / num_trials
print estimate_mystery(10000)
This program uses random.random()
to generates a random set of points that are uniformly distributed over the square with corners at
(1, 1) (−1, 1)
(1,−1) (−1,−1)
Here, being uniformly distribution means that each point in the square has an equal chance of being generated. The method then tests whether these points lie inside a unit circle.
As one increases the number of trials, the value returned by estimate_mystery
tends towards a specific value that has a simple expression involving a well-known constant. Enter this value as a math expression below. (Do not enter a floating point number.)
回答1:
So you need to run estimate_mystery with increasingly higher numbers of trials. As you do so, it will become clear that the value increases to the following simple expression:

(\sum_{k=1}^{\infty} \frac{e^{i\pi(k+1)}}{2k-1}
)
It should be noted, however, that this is not the only correct answer. The following would have been valid too, where \zeta
is the Riemann zeta function:

However, this does not include the well-known constant e
.
I'm not sure why this is confusing. It's quite clear that the sum expression is correct, and it's written quite clearly: the code below the image is very standard LaTeX formatting for mathematical expressions. But to illustrate its correctness, here's a plot showing the convergence when taking the sum to n, and running estimate_mystery up to n as well:

Hrmm... maybe this wasn't what your question wanted? It should also converge to the following, where \gamma
is a unit circle around z=0
on the complex plane:

(-i\oint_\gamma z^{-3}e^{\frac{z}{2}}dz
)
来源:https://stackoverflow.com/questions/28981750/monte-carlo-simulations-by-large-no-of-trials