Chapter 10
Simulation
Consider the problem of finding the area under the curve f(x) = e
−x
2
between
x = 0 and x = 2:
0.5
1.0
1.5
2.0
0.2
0.4
0.6
0.8
1.0
Figure 10.1: Graph of f(x) = e
−x
2
.
Even if you know calculus, this is a difficult task. However, a relatively simple
idea will allow us to approximate the area.
The idea begins with putting a box around the graph that contains it com-
pletely. For a function that stays positive, this means finding a maximum value
m; in this case, we can use m = 1:
0.5
1.0
1.5
2.0
0.2
0.4
0.6
0.8
1.0
Figure 10.2: Box containing area.
Now imagine throwing random darts into the box. Some will land below the
graph, in the area we want

Get A Concise Introduction to Programming in Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.