Random whole numbers with ROUND and RANDOM

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

This Random whole numbers with ROUND and RANDOM worksheet is part of the IGCSE Computer Science 0478 — 8.1c Procedures and Functions journey. Print it as a booklet?

Random whole numbers with ROUND and RANDOM

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which expression gives a random whole number from 0 to 6?
A ROUND(RANDOM() * 6, 0)
B RANDOM() * 6
2. What does RANDOM() give on its own?
A A whole number from 1 to 6
B A decimal from 0 to 1
3. Which line stores a random whole number from 0 to 10?
A Value ← ROUND(RANDOM() * 10, 0)
B Value ← ROUND(RANDOM(), 10)
4. In ROUND(RANDOM() * 6, 0), what does the 0 mean?
A The smallest number that can be picked
B Round to 0 decimal places, so a whole number
5. Which expression gives a random whole number from 1 to 7?
A ROUND(RANDOM() * 6, 0) + 1
B ROUND(RANDOM() * 6, 0) * 1

Match the Pairs

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

Left columnRight column
1Value ← ROUND(RANDOM() * 6, 0) AFUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
2Roll ← ROUND(RANDOM() * 5, 0) + 1 BROUND(RANDOM() * 10, 0)
3Nesting one routine inside another: RANDOM() is called inside ROUND() to get a whole number CStores a random whole number from 0 to 6 in the variable Value
4A function that returns a die roll from 1 to 6 DStores a random whole number from 1 to 6, like a die roll
5Value ← ROUND(RANDOM(), 0) * 6 EThis gives 1 to 7, not 1 to 6, because the range 0 to 6 has 7 values and adding 1 shifts it to 7. To roll a die, multiply by 5 instead: ROUND(RANDOM() * 5, 0) + 1.
6Roll ← ROUND(RANDOM() * 6, 0) + 1 FROUND is applied before multiplying. RANDOM() is a decimal from 0 to 1, so it rounds to only 0 or 1, giving just 0 or 6. The multiplication should be inside ROUND: ROUND(RANDOM() * 6, 0).

Definitions

ROUND(RANDOM() * 6, 0)

Fill in the Blanks

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

1.
Value ← ROUND(______() * 6, 0)
2.
// random whole number 0 to 10 Value ← ROUND(RANDOM() * __, 0)
3.
// random whole number 1 to 6 Value ← ROUND(RANDOM() * 5, 0) + _
4.
FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, _) + 1 ENDFUNCTION
5.
Value ← _____(RANDOM() * 6, 0)

Spot the Error

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

Question 1
A DECLARE Roll : INTEGER
B Roll ← ROUND(RANDOM() * 6)
C OUTPUT Roll
Question 2
A FUNCTION Die() RETURNS INTEGER
B DECLARE N : INTEGER
C N ← ROUND(RANDOM()*5, 0) + 1
D OUTPUT N
E ENDFUNCTION
Question 3
A DECLARE Dice : INTEGER
B Dice ← ROUND(RANDOM() * 5, 0)
C Dice ← Dice * 1
D OUTPUT Dice
Question 4
A Roll ← ROUND(RANDOM * 6, 0)
B Bonus ← Roll * 10
C OUTPUT Bonus
Question 5
A Num ← ROUND(RANDOM(), 0) * 10
B OUTPUT "Number: ", Num
C OUTPUT "Max is 10"

Answer Key

Random whole numbers with ROUND and RANDOM
Section A: Quick Questions
Q1
A — ROUND(RANDOM() * 6, 0)
Q2
B — A decimal from 0 to 1
Q3
A — Value ← ROUND(RANDOM() * 10, 0)
Q4
B — Round to 0 decimal places, so a whole number
Q5
A — ROUND(RANDOM() * 6, 0) + 1
Section B: Match the Pairs
Q1
Value ← ROUND(RANDOM() * 6, 0) → Stores a random whole number from 0 to 6 in the variable Value
Q2
Roll ← ROUND(RANDOM() * 5, 0) + 1 → Stores a random whole number from 1 to 6, like a die roll
Q3
Nesting one routine inside another: RANDOM() is called inside ROUND() to get a whole number → ROUND(RANDOM() * 10, 0)
Q4
A function that returns a die roll from 1 to 6 → FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
Q5
Value ← ROUND(RANDOM(), 0) * 6 → ROUND is applied before multiplying. RANDOM() is a decimal from 0 to 1, so it rounds to only 0 or 1, giving just 0 or 6. The multiplication should be inside ROUND: ROUND(RANDOM() * 6, 0).
Q6
Roll ← ROUND(RANDOM() * 6, 0) + 1 → This gives 1 to 7, not 1 to 6, because the range 0 to 6 has 7 values and adding 1 shifts it to 7. To roll a die, multiply by 5 instead: ROUND(RANDOM() * 5, 0) + 1.
Section C: Definitions
Q1
ROUND(RANDOM() * 6, 0) — Gives a random whole number from 0 to 6. RANDOM() gives a decimal from 0 to 1, times 6 stretches it, and ROUND(..., 0) turns it into a whole number.
Section D: Fill in the Blanks
Q1
Value ← ROUND(RANDOM() * 6, 0)
Q2
// random whole number 0 to 10 Value ← ROUND(RANDOM() * 10, 0)
Q3
// random whole number 1 to 6 Value ← ROUND(RANDOM() * 5, 0) + 1
Q4
FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
Q5
Value ← ROUND(RANDOM() * 6, 0)
Section E: Spot the Error
Q1
B — ROUND needs a second argument saying how many decimal places to keep. For a whole number it must be ROUND(RANDOM() * 6, 0).
Q2
D — A function must pass its result back to the caller with RETURN. OUTPUT only displays the value, so line 4 should be RETURN N.
Q3
C — To move the range from 0 to 5 up to 1 to 6 you must add 1. Multiplying by 1 changes nothing. It should be Dice ← Dice + 1.
Q4
A — RANDOM is a library routine, so it needs brackets when it is called. It should be RANDOM(), giving ROUND(RANDOM() * 6, 0).
Q5
A — The multiplication must happen before rounding. Here RANDOM() is rounded first to 0 or 1, so the result is only 0 or 10. It should be ROUND(RANDOM() * 10, 0).