I am currently reading Introduction to Python Programming by John Zelle. I have gotten to the chapter about simulations and finished all the practice problems except one. This is the problem:

(Advanced) Here is a puzzle problem that can be solved with either some fancy analytic geometry (calculus) or a (relatively) simple simulation. Suppose you are located at the exact center of a cube. If you could look all around you in every direction, each wall of the cube would occupy 1/6 of your ﬁeld of vision. Suppose you move toward one of the walls so that you are now half-way between it and the center of the cube. What fraction of your

ﬁeld of vision is now taken up by the closest wall? Hint: use a Monte Carlo simulation that repeatedly “looks” in a random direction and counts how many times it sees the wall.

I would normally provide code along with this of what I have done but it's proven hard for me to even tackle this problem. I thought of how it would be done for a square instead of a cube.

# i would import math

# i would center a square of length 1 at (0, 0)

# i would make a point at (0, .5)

# from that point I would use

- Code: Select all
`angle = random() * 2 * math.pi`

# Make a line going in the direction of the angle

# x = x + cos(angle)

y = y + sin(angle)

# and figure out which side of the square it intersects and record it

I'm not sure what exactly to do for a cube which is three dimensional. I'd really appreciate some direction.