A simulation is a program that models a real situation, an abstraction that keeps the features that matter and leaves the rest out. Many simulations use RANDOM(a, b), which gives a whole number from a to b with both ends included, to stand in for chance. In AP® Computer Science Principles, you need to explain what simulations are good for, what they leave out, and every result a code segment with RANDOM can produce.
Quick check
What is 17 MOD 5?Show answerHide answer
- 17 = 3 × 5 + 2, so the remainder is 2.
How many different values can RANDOM(1, 6) return?Show answerHide answer
6: the whole numbers 1, 2, 3, 4, 5 and 6. Both ends are included.
Know these already? Jump to the practice questions
What you'll be able to do
- Explain why a simulation is an abstraction of a real situation.
- Describe what simulations are useful for and what they can leave out.
- Determine every value a code segment that uses
RANDOMcan display. - Identify the condition that is true with a given chance.
Key terms
Simulation
A program that imitates a real process or situation so you can study it, often using random values to model chance.
- In plain English
- A computer model you can run again and again.
- Don't write
- "A simulation gives the exact real-world result."
Bias (in a simulation)
A systematic difference between a simulation's results and reality, caused by what the model leaves out or assumes.
- In plain English
- The model is wrong in the same direction every time.
- Don't write
- "Running more trials removes bias."
What makes a simulation an abstraction?
A real situation has endless detail. A simulation keeps only what affects the question being asked. A model of a school cafeteria line might keep how often students arrive and how long each one takes to be served, and drop their names, their lunch choices and the weather.
Chance enters through random values. Instead of knowing exactly when the next student arrives, the model might pick a gap with RANDOM(1, 4) minutes. Each run gives different results, so you run the simulation many times and look at the pattern, such as the average wait.
Key idea
A simulation is only as good as its model: what it keeps, what it drops and what it assumes.
Check yourself
Show answerHide answer
A car's color does not affect how traffic flows, so leaving it out simplifies the model without changing the answer.
What are simulations good for, and what do they miss?
Simulations are useful when a real test would be:
- dangerous, such as testing how a structure fails under extreme loads;
- expensive, because each real trial uses materials or equipment;
- slow, because the real process takes years;
- impossible, because the thing being tested does not exist yet.
They also let you change one condition at a time and repeat a trial thousands of times.
Their limits come from the same simplifications. If a model leaves out something that matters, or makes a wrong assumption, its results are biased however many times you run it. Results that use random values are estimates, not exact answers. A model can be improved by comparing its results with real data and changing its assumptions.
Check yourself
Show answerHide answer
Real customers take different amounts of time, so the model can underestimate how long lines grow when several slow customers arrive together.
What values can a RANDOM expression produce?
RANDOM(a, b) is on the exam reference sheet: it returns a whole number from a to b, including a and b. Treat every value as equally likely (Learn this). It has b − a + 1 possible values, so RANDOM(3, 7) has 5.
To find every result of an expression, list the values RANDOM can return, then apply the expression to each:
| Expression | Possible results |
|---|---|
RANDOM(0, 3) + 5 | 5, 6, 7, 8 |
RANDOM(1, 6) * 2 | 2, 4, 6, 8, 10, 12 (odd numbers never appear) |
RANDOM(1, 6) + RANDOM(1, 6) | 2 to 12, with 7 the most likely |
Two separate calls are independent, so totals in the middle can be made in more ways: 7 comes from 6 of the 36 pairs, but 2 comes only from 1 + 1.
Check yourself
Show answerHide answer
No. Doubling a whole number always gives an even number.
How do you simulate an event with a given chance?
Pick a range, then a condition that is true for the right number of its values. The chance is the number of values that make the condition true divided by the number of values in the range.
- A 3 in 10 chance:
RANDOM(1, 10) ≤ 3is true for 1, 2 and 3, so 3 out of 10. - A 1 in 4 chance:
RANDOM(1, 100) ≤ 25is true for 25 out of 100.
Count the range carefully: RANDOM(0, 10) has 11 values, so RANDOM(0, 10) ≤ 3 is true 4 out of 11 times.
Examiner's eye
Marks are lost when a range is counted as b − a instead of b − a + 1. Count both ends.
Check yourself
Show answerHide answer
1 in 5. Only one of the five possible values is 5.
Worked example 1: List every result
Question: The code segment below runs once. What are (a) the smallest and (b) the largest value it can display, and (c) how many different values can it display?
x ← RANDOM(1, 4)
y ← x * 3 - 1
DISPLAY(y)
What tells you the method
One RANDOM call feeding an expression: list every value of x, then compute y for each.
Step 1: Tabulate every case.
x1 2 3 4 y2 5 8 11 WhyRANDOM(1, 4)can return each whole number from 1 to 4, and*happens before-.Step 2: Read off the answers.
The smallest is 2, the largest is 11, and there are 4 different values.
WhyEach
xgives a differenty, so the count of results equals the count ofxvalues.
Check it
The values go up by 3 each time, matching the * 3, and 7 can never appear.
Answer
(a) 2 (b) 11 (c) 4
Worked example 2: Two random values
Question: The code segment below runs once. (a) How many different values can it display? (b) Which value is it most likely to display?
a ← RANDOM(1, 3)
b ← RANDOM(1, 3)
DISPLAY(a + b)
What tells you the method
Two separate calls: list every pair, because each pair is equally likely.
Step 1: List all 9 pairs and their totals.
a\b1 2 3 1 2 3 4 2 3 4 5 3 4 5 6 Why3 values for
atimes 3 values forbgives 9 equally likely pairs.Step 2: Count each total.
2 appears once, 3 twice, 4 three times, 5 twice and 6 once.
WhyThe most likely total is the one made by the most pairs.
Check it
1 + 2 + 3 + 2 + 1 = 9, so every pair was counted once.
Answer
(a) 5 (b) 4
Worked example 3: Choose the right condition
Question: A game simulation needs a condition that is true with a chance of exactly 3 in 10. Which condition does that?
- A)
RANDOM(0, 10) ≤ 3 - B)
RANDOM(1, 10) < 3 - C)
RANDOM(1, 10) ≤ 3 - D)
RANDOM(1, 3) = 1
What tells you the method
A "which condition" question: for each option, count the values in the range and the values that make it true.
Step 1: Count each option.
Option Values in range Values that make it true Chance A 11 0, 1, 2, 3 4 in 11 B 10 1, 2 2 in 10 C 10 1, 2, 3 3 in 10 D 3 1 1 in 3 WhyThe chance is the true values divided by all the values.
Check it
C's range has exactly 10 values and exactly 3 of them are at most 3.
Answer
C
Your turn
Question: The code segment below runs once. (a) How many different values can it display? (b) Which value has the greatest chance of being displayed?
n ← RANDOM(2, 5)
DISPLAY(n MOD 3)
Step 1:
ncan be 2, 3, 4 or 5, each equally likely.Step 2: Your go: work out
n MOD 3for each value ofn, then count.
Show the full solutionHide the solution
Step 2. 2 MOD 3 = 2, 3 MOD 3 = 0, 4 MOD 3 = 1 and 5 MOD 3 = 2. The displayed value is 0, 1 or 2, and 2 comes from two of the four values of n.
Answer: (a) 3 (b) 2
Traps
Trap
RANDOM(1, 10) can never return 10.
It seems right because a range can sound as if it stops before its end. But RANDOM(a, b) includes both a and b, so 10 is as likely as any other value.
Can RANDOM(1, 10) return 10?
Yes. Both ends are included.
Trap
RANDOM(1, 6) * 2 can give any whole number from 2 to 12.
It seems right because 2 and 12 are the smallest and largest results. But multiplying by 2 skips every odd number, so list each case instead of assuming every value between the ends appears.
Which of these can RANDOM(1, 6) * 2 give: 9 or 10?
10, from 5 × 2. No whole number doubled gives 9.
Trap
A simulation's output is the true real-world answer.
It seems right because a computer gives precise-looking numbers. But the model leaves things out and makes assumptions, and random runs vary, so the output is an estimate.
Does running a biased simulation 10,000 times remove the bias?
No. More runs reduce the run-to-run variation, but every run uses the same flawed model.
What's examined
On the AP® Computer Science Principles exam, simulations and RANDOM belong to Big Idea 3: Algorithms and Programming. Expect multiple-choice questions on why a simulation would be used, what a model leaves out, and which values a code segment with RANDOM can or cannot display.
Practice questions
- Recall Which is the best reason to use a simulation instead of a real experiment?
- A) The real experiment would be too dangerous or costly to repeat many times.
- B) A simulation always gives the exact real-world result.
- C) A simulation never needs any assumptions.
- D) A simulation cannot use random values, so it is more reliable.
Show answer
Answer: A. Simulations let you repeat trials safely and cheaply.
Why the others are wrong:
- B: a simulation gives an estimate from a simplified model.
- C: every model rests on assumptions.
- D: simulations often use random values.
- Apply How many different values can
RANDOM(5, 12)return?
Show answer
Answer: 8
Marking points:
- The count is 12 − 5 + 1 = 8: the values 5 to 12, both ends included.
- Apply The statement
DISPLAY(RANDOM(1, 5) * 2 + 1)runs once. Which value can never be displayed?
- A) 5
- B) 8
- C) 9
- D) 11
Show answer
Answer: B. The possible values are 3, 5, 7, 9 and 11, all odd, so 8 can never appear.
Why the others are wrong:
- A comes from
RANDOMreturning 2. - C comes from 4.
- D comes from 5, the largest value; ranges include their top end.
- Apply In the long run, what fraction of the time is
RANDOM(1, 8) > 6true? Give your answer as a decimal.
Show answer
Answer: 0.25
Marking points:
- Only 7 and 8 are greater than 6: 2 of the 8 values.
- 2 ÷ 8 = 0.25.
- Exam-style A simulation of a cafeteria line uses random values for when students arrive, but assumes every student takes exactly 2 minutes to be served. Which statement best describes a limitation of this simulation?
- A) It cannot be run on a computer, because lines involve people.
- B) It must give the same result every time it runs.
- C) It cannot model arrivals, because arrivals involve chance.
- D) Real serving times vary, so its predicted waits may differ from real waits.
Show answer
Answer: D. The fixed 2 minutes is a simplifying assumption, and a wrong assumption can bias the results.
Why the others are wrong:
- A: any process with clear rules can be modeled.
- B: the arrivals use random values, so results vary between runs.
- C: the simulation already models arrivals with random values.
- Exam-style The code segment below runs once. How many different values can it display?
total ← 0
REPEAT 3 TIMES
{
total ← total + RANDOM(0, 1)
}
DISPLAY(total)
Show answer
Answer: 4
Marking points:
- Each pass adds 0 or 1, so after 3 passes
totalis anything from 0 to 3. - Every value in between can happen, for example 2 from 1 + 1 + 0, so there are 4 values: 0, 1, 2 and 3.
- Exam-style The code segment below should simulate 100 rolls of a six-sided number cube and display how many rolls were 6. Give the number of the line that stops it working.
1: count ← 0
2: REPEAT 100 TIMES
3: {
4: roll ← RANDOM(1, 5)
5: IF(roll = 6)
6: {
7: count ← count + 1
8: }
9: }
10: DISPLAY(count)
Show answer
Answer: Line 4.
Marking points:
RANDOM(1, 5)never returns 6, socountstays 0 and 0 is always displayed.- The fix is
roll ← RANDOM(1, 6).
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- A simulation models a real situation by keeping what matters and dropping the rest.
- Simulations make dangerous, costly, slow or impossible tests possible, and repeatable.
- Leaving out what matters, or assuming wrongly, biases the results however often you run it.
RANDOM(a, b)includes both ends and has b − a + 1 values.- To find every result, list each value
RANDOMcan return and apply the expression to each.