Main content

## AP®︎/College Computer Science Principles

### Course: AP®︎/College Computer Science Principles > Unit 6

Lesson 3: Creating simulations# Programming a simple simulation

AP.CSP:

AAP‑3 (EU)

, AAP‑3.F (LO)

, AAP‑3.F.1 (EK)

, AAP‑3.F.2 (EK)

, AAP‑3.F.3 (EK)

, AAP‑3.F.4 (EK)

, AAP‑3.F.8 (EK)

Ever heard the fable about the tortoise and the hare?

A hare makes fun of a tortoise for moving so slowly and challenges the tortoise to a race. The hare speeds ahead in the race, but then seeing that he's so far ahead, he takes a nap. The hare wakes up to discover that the tortoise had slowly but surely overcome him. 🐢> 🐇 !

The fable has inspired people to run actual races between tortoises and hares. I don't have any such animals handy, but I am curious to see if it's really possible: can a tortoise beat a sleepy hare? How fast does must the tortoise be and how sleepy must the hare be?

Let's investigate the questions by programming a simulation.

## Starting simple

We'll begin by simulating a race between a tortoise and a hare that

*doesn't*take naps. I think we both know the results of that race, but when we're programming, it helps to start with a simpler problem and then add complexity after.First we need to establish some initial conditions:

- The length of the race
- The average speed of the tortoise
- The average speed of the hare

We also need to keep track of some values that vary during the race:

- The current position of the tortoise
- The current position of the hare

We can store both the initial conditions and the varying values in variables.

Here's JavaScript code that initializes those variables:

```
var tortoiseSpeed = 1;
var hareSpeed = 2;
var raceDistance = 350;
var tortoisePos = 0;
var harePos = 0;
```

Then we can use a loop that updates the position of the tortoise and the hare and only stops once one of them reaches the finish line.

```
while (tortoisePos < raceDistance && harePos < raceDistance) {
tortoisePos += tortoiseSpeed;
harePos += hareSpeed;
}
```

Here's the running simulation with that code:

As long as the hare is faster than the tortoise, it

*will*win that race. Now that we've got that working, it's time to make it more interesting.## Adding complexity

Our simulation needs to model the behavior of a lazy hare. We can start by establishing some initial properties about his laziness:

- How long is he typically awake between naps?
- When he does nap, how long are the naps?

Here's JavaScript code that stores those properties as variables:

```
var minsBetweenNaps = 30;
var napLength = 20;
```

To figure out when to stop the hare for naptime, we need to also keep track of some values:

- How long has it been since the last nap?
- If he's currently napping, how long has it been since the nap started?

This code stores initial values for those variables:

```
var minsSinceLastNap = 0;
var minsSinceNapStart = 0;
```

The hare starts off awake and running, so both variables start at 0.

Finally, we need to add logic inside the loop to make the hare take naps.

If the hare doesn't need to nap yet, the code updates the hare position (like before) and increments

`minsSinceLastNap`

:```
if (minsSinceLastNap <= minsBetweenNaps) {
harePos += hareSpeed;
minsSinceLastNap++;
}
```

If the hare should be napping, the code increments

`minsSinceNapStart`

, and once the hare has finished napping, the code resets `minsSinceLastNap`

and `minsSinceNapStart`

to 0:```
else {
if (minsSinceNapStart < napLength) {
minsSinceNapStart++;
} else {
minsSinceLastNap = 0;
minsSinceNapStart = 0;
}
}
```

## Visualizing the changes

A simulation does

*not*have to have a visual output; it may just have a textual or numeric output like the simulations above. However, a visualization can help the users of a simulation gain a more intuitive understanding of the output.The way in which we add a visual component to a computer simulation depends on the language we're using and what libraries we have available. In the Khan Academy coding environment, we can use the ProcessingJS library to draw and animate shapes onto the screen, so that's a great fit for visualizing this simulation.

This simulation runs the same logic as before but adds a visualization:

## Adding variability

The real world is full of variation. Tortoises and hares vary in their speeds; the slowest tortoise moves at 0.3 km/hr while the fastest moves at 5 km/hr. Plus, within a race, the tortoise and hare won't move at exactly the same speed the whole time; they'll have their fast moments and slow moments.

One way to add variation to a simulation is to generate random numbers. We can find out the expected ranges of a value in the real world and generate values within those ranges.

Try to find all the places that use randomly generated numbers in this version of the simulation, and then restart to see what changes:

Note that this simulation uses more realistic numbers for the speed of the hare versus the tortoise, so you might be a bit shocked by the results. Play around with the nap parameters to see if you can give the tortoise a shot at winning the race.

## What's excluded?

This simulation is an abstraction of a real world scenario. It attempts to include the most significant parameters about the scenario, but it cannot include everything.

Here are some ideas for excluded details that might affect the race:

- whether each racer moves in a straight path or meanders
- whether each racer has the calories necessary to keep racing
- whether either racer risks overheating during the race

🤔 What else is the simulation missing? How could the simulation be more realistic? What parameters would let us investigate more possibilities?

🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer—just ask in the questions area below!

## Want to join the conversation?

- what does it mean to add variation to a simulation(3 votes)
- We can use randomness to change aspects of the simulation and add some variation. For example, we could randomize the color of a flower or the path of a ball. This can make the simulation more interesting as it would change every time we would run it.(3 votes)