< Unit 3 - Interactive Animations and Games ('22-'23)

Lesson 6: Random Numbers

45 minutes

Overview

Question of the Day: How can we make our programs behave differently each time they are run?

Students are introduced to the randomNumber() block and how it can be used to create new behaviors in their programs. They then learn how to update variables during a program. Combining all of these skills, students draw randomized images.

Purpose

This lesson introduces randomness, which is important both as a way to make programs more interesting, but also to motivate the use of variables. In the middle of the activity, students are exposed to a variable that is updated multiple times in the program, expanding their understanding of how variables can be used.

Assessment Opportunities

1. Generate and use random numbers in a program

See level 6 in Code Studio.

2. Update a value stored in a variable

See level 4 in Code Studio. Check that students have updated the value of "petalSize" between drawing the two flowers.

CSTA K-12 Computer Science Standards (2017)
    • 2-AP-11 - Create clearly named variables that represent different data types and perform operations on their values.
    • 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
    • 2-AP-17 - Systematically test and refine programs using a range of test cases.
    • 2-AP-19 - Document programs in order to make them easier to follow, test, and debug.

Agenda

Objectives

Students will be able to:
  • Generate and use random numbers in a program
  • Update a value stored in a variable

Preparation

  • Review the level progression in Code Studio
  • Check the "Teacher's Lounge" forum for verified teachers to find additional strategies or resources shared by fellow teachers
  • If you are teaching virtually, consider checking our Virtual Lesson Modifications

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the teachers

Teaching Guide

Warm Up (5 minutes)

Prompt: So far, our programs have done the same thing every time that we run them. Are there any times that you'd want a program to do something differently each time it was run?

Discuss: Allow students time to write down some ideas, then discuss as a group.

Discussion Goal

The goal of this discussion is to set context for the introduction of random numbers. Students may come up with various ideas related to user interaction or gathering input from other sources. Allow them to discuss the different ideas that they have, but eventually turn the conversation to the idea of randomness.

Remarks

So far, we've wanted our programs to do exactly as we've coded, and most of our surprises have been bugs. Today we're going to look at how we can code random behaviors into our programs so that we can get some good surprises.

Question of the Day: How can we make our programs behave differently each time they are run?

Activity (35 minutes)

Programming Images

Transition: Move students onto Code Studio

Teaching Tip

Guide to Programming Levels: Additional guidance for programming levels is provided in the CSD Guide to Programming Levels. This document includes strategies and best-practices for facilitating programming levels with students.

Assessment Opportunity

Formative Assessment: This level can be used as a formative assessment. A rubric is provided in the level, and written feedback can be given to students. Click here to learn more about giving feedback to students.

Share: If some students have taken extra time to work on their projects, give them a chance to share their more complex rainbow snakes. Focus conversation on which parameters students are manipulating or randomizing to create their drawings.

Wrap Up (5 minutes)

Question of the Day: How can we make our programs behave differently each time they are run?

Prompt: So far, we've only looked at random numbers. Are there any other things that you might like to be random in your program?

Share: Allow students to share out what sorts of random things they might like in their programs.

Discussion Goal

The discussion is intended to have students think about the wider implications of randomness in games and other programs. Although there is no block to general random data other than numbers, in later lessons students will learn techniques that will allow them to use random numbers to randomly choose from a variety of behaviors.

Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.