Scratch Programming
Random Numbers

Introduction

Scratch can generate random numbers for you to use in your programs. Random numbers are useful when you write programs that simulate random events in the real world. They are also useful when creating challenges in games.

Example 1

This program is a guessing game. The program generates a random number and gives the user three goes at guessing it.

Scratch Program

Challenge 1: Copy and test the program. Change the program so that the number generated is from 1 to 100. Give the player as many goes at guessing the number as they need. At the end, tell them how many guesses it took them.

Challenge 2: Adapt the program further. Now make it so the program tells them if they were too high or too low with their guess. With a given range of random numbers, there is a limit to the number of guesses needed if the program gives you feedback about whether guesses are too high or too low. Try to explain this in your own words.

Example 2

This program is an animated 6-sided die. The die sprite has 6 costumes, one for each number on the die.

Scratch Program

This short script animates the die and makes sure that the result is random.

Scratch Program

Challenge 1: Improve the quality of the costumes in the program so that dot patterns are used.

Challenge 2: The program works by switching the costumes on the die sprite. Can you work out how to get the program to say the number the die settled on?

Challenge: A Simple Game

This game is called Catch the clown. When the game starts, the clown points in a random direction and starts moving at constant speed. When it hits a wall it bounces off. If the player clicks on the clown they get a point on their score, the clown disappears and reappears in a random position and speeds up slightly.

Scratch Program

Your challenge is to make a game like this. You only need one sprite, one green flag script and one When Sprite Clicked script to achieve this effect.

When you have completed this, think of ways you can make the game a little more interesting.