RANDOM: a random number between 0 and 1

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

RANDOM: a random number between 0 and 1

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which call correctly uses the RANDOM library routine?
A Num ← RANDOM(10)
B Num ← RANDOM()
2. What range does RANDOM() return values in?
A From 0 to 1 inclusive
B From 1 to 10 inclusive
3. Which statement about the result of RANDOM() is true?
A It is a REAL number, such as 0.37
B It is always an INTEGER, either 0 or 1
4. RANDOM() is called twice in a program. What should you expect?
A A different value each time it is called
B The same value both times
5. Which line stores a random number in a variable called Chance?
A RANDOM() ← Chance
B Chance ← RANDOM()

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1RANDOM() AValue ← RANDOM()
2Num ← RANDOM() BDECLARE Chance : REAL
3RANDOM takes no arguments, so its brackets are left empty when it is called CA library routine that returns a random REAL number between 0 and 1 inclusive, using empty brackets
4The result of RANDOM is a REAL number, so the variable that stores it should be declared as REAL DRANDOM takes no arguments, so a value must not be put inside the brackets. It should be RANDOM().
5Num ← RANDOM(10) EThe brackets are missing. RANDOM is a routine, so it must be called with empty brackets: RANDOM().
6Num ← RANDOM FCalls RANDOM and stores the returned random REAL value in the variable Num

Definitions

RANDOM()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Num ← ______()
2.
_______ Num : REAL Num ← RANDOM() OUTPUT Num
3.
Value ← RANDOM() OUTPUT _____
4.
Num _ RANDOM()
5.
DECLARE Chance : ____ Chance ← RANDOM() OUTPUT Chance

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Num : INTEGER
B Num ← RANDOM()
C OUTPUT Num
Question 2
A DECLARE Chance : REAL
B Chance ← RANDOM(10)
C OUTPUT Chance
Question 3
A DECLARE First, Second : REAL
B First ← RANDOM()
C OUTPUT First
D Second ← RANDOM
Question 4
A DECLARE Num : REAL
B Num ← RANDOM()
C IF Num <= 1
D OUTPUT "In range"
E ENDIF
Question 5
A DECLARE Luck : REAL
B RANDOM() ← Luck
C OUTPUT Luck

Answer Key

RANDOM: a random number between 0 and 1
Section A: Quick Questions
Q1
B — Num ← RANDOM()
Q2
A — From 0 to 1 inclusive
Q3
A — It is a REAL number, such as 0.37
Q4
A — A different value each time it is called
Q5
B — Chance ← RANDOM()
Section B: Match the Pairs
Q1
RANDOM() → A library routine that returns a random REAL number between 0 and 1 inclusive, using empty brackets
Q2
Num ← RANDOM() → Calls RANDOM and stores the returned random REAL value in the variable Num
Q3
RANDOM takes no arguments, so its brackets are left empty when it is called → Value ← RANDOM()
Q4
The result of RANDOM is a REAL number, so the variable that stores it should be declared as REAL → DECLARE Chance : REAL
Q5
Num ← RANDOM(10) → RANDOM takes no arguments, so a value must not be put inside the brackets. It should be RANDOM().
Q6
Num ← RANDOM → The brackets are missing. RANDOM is a routine, so it must be called with empty brackets: RANDOM().
Section C: Definitions
Q1
RANDOM() — A library routine with empty brackets that returns a random REAL number from 0 to 1 inclusive, a different value each time it is called. Store it, e.g. Num ← RANDOM()
Section D: Fill in the Blanks
Q1
Num ← RANDOM()
Q2
DECLARE Num : REAL Num ← RANDOM() OUTPUT Num
Q3
Value ← RANDOM() OUTPUT Value
Q4
Num ← RANDOM()
Q5
DECLARE Chance : REAL Chance ← RANDOM() OUTPUT Chance
Section E: Spot the Error
Q1
A — RANDOM() returns a REAL number between 0 and 1, so the variable that stores it must be declared as REAL, not INTEGER.
Q2
B — RANDOM() takes no arguments, so the brackets must be empty. It should be Chance ← RANDOM().
Q3
D — A routine call needs brackets, even when there are no arguments. It should be Second ← RANDOM().
Q4
C — An IF statement must have THEN after its condition. It should be IF Num <= 1 THEN.
Q5
B — The assignment arrow points from the value to the variable. The result of RANDOM() is stored with Luck ← RANDOM().