Question bank › GCSE › GCSE Computer Science › Programming

Input/output & random number generation

10 questions43 marksabout 50 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Grade 8–96 marks

Write a program, using AQA pseudocode, that simulates rolling two six-sided dice. The program should:

  • keep rolling both dice until a double (both dice the same value) is rolled
  • then output how many rolls were needed.

You should use the subroutine RANDOM_INT(a, b), which returns a random integer between a and b inclusive. [6]

2Grade 1–32 marks

A program contains this line:

name ← USERINPUT

(a) Describe what happens when this line is executed. [1]

The program later contains this line:

OUTPUT "Hello " + name

(b) The user typed Priya when asked for their name. State the exact output of this line. [1]

3Grade 1–33 marks

Lines 1 to 6 of a program are shown below. The line numbers are not part of the program.

1  OUTPUT "Enter the length"
2  length ← STRING_TO_INT(USERINPUT)
3  OUTPUT "Enter the width"
4  width ← STRING_TO_INT(USERINPUT)
5  area ← length * width
6  OUTPUT "Area is " + INT_TO_STRING(area)

(a) State the line number of one line where the program takes input from the keyboard. [1]

(b) The user enters 7 and then 4. State the exact output of line 6. [1]

(c) Give one reason why STRING_TO_INT is needed on line 2. [1]

Stuck on Input/output & random number generation?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Grade 4–54 marks

In AQA pseudo-code, RANDOM_INT(a, b) returns a random integer between a and b inclusive.

(a) State how many different values RANDOM_INT(5, 12) can return. [1]

(b) Write an expression, using RANDOM_INT, that returns a random multiple of 5 between 5 and 50 inclusive. [1]

(c) A student wants to simulate rolling a fair 12-sided dice numbered 1 to 12. They write:

roll ← RANDOM_INT(1, 6) + RANDOM_INT(1, 6)

Explain why this does not correctly simulate the dice. [2]

5Grade 4–54 marks

A program converts a temperature entered by the user from Celsius to Fahrenheit. Two parts of the program are missing.

OUTPUT "Enter the temperature in Celsius"
celsius ← STRING_TO_REAL( ① )
fahrenheit ← celsius * 1.8 + 32
OUTPUT "In Fahrenheit: " + ② (fahrenheit)

(a) Complete the program by stating what should replace ① and ②. [2]

(b) The user enters 25. State the value stored in fahrenheit. [1]

(c) Give one reason why STRING_TO_REAL is used rather than STRING_TO_INT. [1]

6Grade 4–53 marks

Sam writes this program.

1  OUTPUT "How old are you?"
2  age ← USERINPUT
3  OUTPUT "Next year you will be " + INT_TO_STRING(age + 1)

When Sam runs the program and types 15, the program stops with a type error on line 3.

(a) Explain why the error occurs. [2]

(b) Rewrite line 2 so that the program works correctly. [1]

7Grade 6–74 marks

A program calls RANDOM_INT(1, 4) 200 times and counts how many times each value is returned. The bar chart shows the results of one run.

(a) Calculate how many times each value would be returned if the 200 results were shared out exactly equally. [1]

(b) Explain why the counts in the chart are not all equal to your answer to (a). [2]

(c) Describe how the chart would be likely to change if the program called RANDOM_INT(1, 4) 20 000 times instead of 200. [1]

0204060481532513484Value returned by RANDOM_INT(1, 4)Number of times
8Grade 6–76 marks

Write a program, using AQA pseudo-code, for a guessing game. The program should:

  • choose a random integer between 1 and 20 inclusive and store it
  • repeatedly ask the user to enter a guess from the keyboard
  • output Too high if the guess is bigger than the stored number and Too low if it is smaller
  • stop and output Correct when the user guesses the stored number.

You should use the subroutine RANDOM_INT(a, b), which returns a random integer between a and b inclusive. [6]

9Grade 6–75 marks

The following program uses RANDOM_INT(a, b), which returns a random integer between a and b inclusive.

total ← 0
FOR i ← 1 TO 4
   roll ← RANDOM_INT(1, 6)
   IF roll MOD 2 = 0 THEN
      total ← total + roll
   ELSE
      total ← total - 1
   ENDIF
ENDFOR
OUTPUT total

(a) For one run of the program, assume that the four calls to RANDOM_INT return 3, 6, 6 and 1, in that order. Complete the trace table for this run. [3]

irolltotal
0
1
2
3
4

(b) State the output of this run. [1]

(c) Give one reason why a programmer testing this program might temporarily replace RANDOM_INT(1, 6) with fixed values. [1]

10Grade 8–96 marks

A program generates a numeric security code. The program should:

  • ask the user to enter the number of digits wanted, and keep asking until a number between 4 and 8 inclusive is entered
  • build a string made of that many random digits, each from 0 to 9
  • output the finished code as a single string.

Write the program using AQA pseudo-code. You should use RANDOM_INT(a, b), which returns a random integer between a and b inclusive. [6]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session