3.14 to 3.16 Libraries, random values and simulationsBorrowed code, chance, and models of the world

Libraries and APIs
A software library contains procedures that can be used in creating new programs. Existing code segments can come from internal sources (code you or your team wrote before) or external ones (libraries other people wrote). Using libraries simplifies creating complex programs: you don't write your own sorting or drawing code. An API, application program interface, is the specification of how the procedures in a library behave and can be used. Documentation for a library or API is necessary to understand what its procedures do and how to call them.
| Library procedure (from its API) | What the documentation promises |
|---|---|
| round(x) | Returns x rounded to the nearest whole number |
| sqrt(x) | Returns the square root of x; x must be at least 0 |
| sortList(aList) | Puts aList in order from smallest to largest |
| drawCircle(x, y, r) | Draws a circle of radius r centered at (x, y) |
?Wait, is a library an example of procedural abstraction?
Random values
RANDOM(a, b) returns a random whole number from a to b, including both a and b, and each result is equally likely. RANDOM(1, 6) is a die roll: 1, 2, 3, 4, 5 or 6. Because of randomness, a program that uses RANDOM may produce a different result each time it runs, so questions often ask what values are possible, or how likely one is.
To find a probability, count: favorable outcomes divided by total equally likely outcomes. RANDOM(1, 10) ≤ 3 is true for 3 of the 10 values, so 30 percent of the time. For two separate random calls, list every pair. Two calls of RANDOM(1, 3) give 3 × 3 = 9 equally likely pairs.
| x \ y | y = 1 | y = 2 | y = 3 |
|---|---|---|---|
| x = 1 | 2 | 3 | 4 |
| x = 2 | 3 | 4 | 5 |
| x = 3 | 4 | 5 | 6 |
?Wait, how many values does RANDOM(a, b) have?
Simulations
Simulations are abstractions of more complex objects or phenomena for a specific purpose. They mimic real-world events, using varying sets of values to reflect a changing state, so we can draw inferences without the constraints of the real world. They're most useful when real experiments are impractical: too big, too small, too fast, too slow, too expensive or too dangerous. Simulations help people form and refine hypotheses, and random number generators can model the variability of the real world.
Building a simulation means removing details and simplifying. That makes it possible, but it has a cost: a simulation can contain bias from the choices of which real-world elements are included or left out. Its results are only as good as its assumptions.
| Real situation | Why simulate it | A detail you might leave out |
|---|---|---|
| Evacuating a stadium | Too dangerous to test for real | People who stop to help others |
| Spread of a flu | Too slow and unethical to wait and watch | Different contact by age |
| A new bus route | Too expensive to build first | Riders who switch to bikes |
| A galaxy forming | Too big and too slow | Most individual stars |
1heads ← 02REPEAT 100 TIMES3{4 IF (RANDOM(1, 2) = 1)15 {6 heads ← heads + 17 }8}9DISPLAY(heads)
- line 4 A fair coin: 1 means heads, 2 means tails
I thought about it. Show me
- (a) The smallest sum is 1 + 1 = 2 and the largest is 3 + 3 = 6, and every sum between is reachable: 2, 3, 4, 5, 6.
- (b) There are 3 × 3 = 9 equally likely (x, y) pairs. The pairs that sum to 4 are (1, 3), (2, 2) and (3, 1): 3 of 9, so 1/3.
Why?
Count pairs, not sums. The sums aren't equally likely; the pairs are. - (c) No. 1 would need a 0, and 7 would need a 4, and RANDOM(1, 3) only gives 1, 2 or 3.
Answer: (a) 2, 3, 4, 5, 6. (b) 3/9 = 1/3. (c) No: the possible sums are 2 through 6 only.
Stuck? Get a hint
- Make a 3 by 3 table: x down the side, y across the top.
- Fill each cell with x + y.
- Count the cells equal to 4, out of 9.
- ARANDOM(1, 5) = 5
- BRANDOM(0, 5) = 0
- CRANDOM(1, 20) = 1
- DRANDOM(1, 10) ≤ 1
Show the answer
A. RANDOM(1, 5) has 5 equally likely values, and 1 of them is 5: 1/5 = 20 percent. RANDOM(0, 5) has 6 values (about 17 percent), RANDOM(1, 20) = 1 is 5 percent, and RANDOM(1, 10) ≤ 1 is 10 percent.
- AThe simulation will run more slowly than the real bridge.
- BThe results may be biased, because elements of the real situation were left out.
- CThe simulation cannot use random numbers.
- DThe simulation becomes an exact copy of reality.
Show the answer
B. Simplifying is what makes simulations possible, but choices about what to include or leave out can bias the results. A bridge designed only for cars might fail the people who walk or bike.
- A library holds reusable procedures. An API specifies how they behave; documentation explains how to use them.
- RANDOM(a, b): a whole number from a to b, inclusive, each equally likely. That's b - a + 1 values.
- Probability: favorable equally likely outcomes ÷ total. For two calls, count pairs.
- Simulations are abstractions for when real tests are impractical. Simplifying can introduce bias.