Skip to main content
← Unit 3: Algorithms and Programming

Code Scholars Tutoring · AP Computer Science Principles

3.5 Simulation and Randomness

How it works

Simulations model systems using rules and assumptions; randomness can help represent uncertainty or variation.

Identify what the simulation models, what is simplified, and which conclusion the results can support.

Common error

A simulation is only as trustworthy as its assumptions and data.

Worked example

Model a fair die using the inclusive AP CSP RANDOM operation.

Data / pseudocode

roll <- RANDOM(1, 6)
success defined as roll <= 2
Possible outcomes: 1, 2, 3, 4, 5, 6

Two of six equally likely outcomes are successes, so the model probability is 1/3. A finite simulation will vary around the model rate; it need not produce exactly one third successes. The fair-die assumption should be stated and checked against the real system being modeled.

Exercises

3.5.1

Use RANDOM(1, 10) to model a 30% chance event. Give the success condition.

Show solution

Use RANDOM(1, 10) <= 3. Exactly three of the ten equally likely integer outcomes count as success.

3.5.2

A traffic simulation assumes every vehicle moves at the same speed. Explain one limit on using it to predict real congestion.

Show solution

Real drivers, intersections, incidents, and vehicle behavior vary. The simplified simulation may help explore its assumptions but cannot establish accurate real-world congestion predictions without validation.

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic