Skip to main content
Skip to main content

Pencil Code Program:
Chaos game

Integrating Digital
Technologies

Years 7-8

DT+ Mathematics


The ‘chaos game’ is a way to see how patterns can result from certain random events.

Use this program to run the ‘chaos game’, randomly moving the turtle to create a pattern (for more information, search 'chaos game’).

Have students analyze or fill in or change parts of the pencil code program.

Based on the original lessons developed by the Exploring Computational Thinking team at Google.


Decorative image

Learning hook

Roll out a bag of marbles onto the floor. Look at the random pattern made. If the bag of marbles was released would the same pattern be made? Ask students to explain.

Discuss random other events and randomisation with relevant examples.

Ask students, ‘Would you expect to see patterns with random events?’ Why or why not?

Introduce the ‘chaos game’ as a way to see how patterns can result from certain random events.

Use the pencil code program to run the ‘chaos game’, randomly moving the turtle to create a pattern (for more information, search 'chaos game’).

Learning demonstration

Provide students with a link to pencilcode.net and the pencil code program. Run the program and ask students to describe their observations. Ask the following questions:

  • What are the parts of the program? What results onscreen?
  • What is the program doing?
  • At what point do you see a pattern emerging?

Learning construction

Have students analyze or fill in or change parts of the pencil code program. This program could be used to further your understanding of how you could use Pencil Code in the classroom, as a demonstration or discussion with your students, or as a way to introduce various CT concepts, such as pattern recognition or abstraction, to your students by inviting them to extend the existing functionality of the program.

Ways to remix the program

  1. Change fd value from 200 to another number for each sprite. What happens when you don’t use the same value for each of the three dots (sprites)?
  2. Change the speed from 1000 to another value
  3. Change the range from 1..2000 to another value eg. 1..50000
  4. Change the value of the factor multiplying the fd distance(p), from 0.5 to another value eg try 0.75 for starters

Computational Thinking Concepts*

* Explore the Computational Thinking Concepts Guide for a list of the CT concepts, including tips for implementing each concept in your classroom.

Pencil Code Program

Copy/Paste the following program into a ‘Blank Editor’ on the Pencil Code website.


# Copyright 2015 Google Inc. All Rights Reserved.
 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
 
# http://www.apache.org/licenses/LICENSE-2.0
 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


v = [
    new Sprite('red dot').fd(200)
    new Sprite('blue dot').rt(120).fd(200)
    new Sprite('green dot').lt(120).fd(200)
]

speed 1000
for [1..2000]
  p = random v
  turnto p
  fd distance(p) * 0.5
  dot black, 2
  await done defer()

Sample output

A sample output using the Pencil Code program showing a geometric pattern that the turtle has created. The turtle seems to be following the same path despite the formula being randomised.

Additional Resources