Random Numbers
Random Numbers
Computers follow instructions and will only do what you tell them to do. They will do the same thing every time you ‘run’ the program. This is a good thing.
However, there are time when you do want your program to do something surprising, for example:
- modelling flipping a coin
- making computer games less predictable
- picking a name out of a group of students.
- shuffling a playlist
To create random events we need to generate a random number.
In scratch we use the ‘pick random’ operator.
This operation will choose a random number between the two numbers that we can then use in our programming.
In the example below the program picks a random number between 1 and 2 (inclusive), and assigns it to a variable called ‘number’.
IF the number is 1 it shows the ‘heads’ otherwise (ELSE) it shows tails.
Click the coin to see the program in action
Watch the video below to see how to use the PICK RANDOM operator block to flip a coin and roll a die.
Challenges
You can download the files for the challenges at the bottom of this page.
Random Maths
Create a program that generates 2 random numbers (between 1 and 100) and stores them in 2 seperate variables.
The Maths Man should then add the two numbers together and say the answer.
Game Show
Create a program that asks the user to pick a prize box between 1 and 6.
The computer should also pick a random box between 1 and 6.
You will need to store their answer in a variable.
IF they pick the right box they win £1000 otherwise (ELSE) they win a cabbage.
The Quizmaster sprite has 3 costumes:
- Asking a question
- Holding a prize
- Holding a cabbage
Maths Quiz
Create a program that generates 2 random numbers (between 1 and 100) and stores them in 2 seperate variables.
The quizmaster should ask the player to add the numbers together.
You will need to store their answer in a variable.
The program should tell the user IF their answer was right or wrong.
Random Shapes
Create a program that will draw random regular polygons (a minimum of 3 sides to a maximum of 18).
Key Words
Random Operator
The random operator generates a random number between two values.