Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
AQA GCSE Computer Science (Paper 1) Smart practice A mixed round aimed at your weak spots
Unit 1
3.1 Fundamentals of algorithms
Unit 2
3.2 Programming
Data types
Data types
Arithmetic operations in a programming language
Arithmetic operations, DIV and MOD
Relational operations in a programming language
Relational operations
Boolean operations in a programming language
Boolean operations in conditions
Data structures
Arrays and records
Input/output
Input and output
String handling operations in a programming language
String handling
Random number generation in a programming language
Random number generation
Structured programming and subroutines (procedures and functions)
Subroutines and structured programming
Avg
score:
–
Topics
done:
0/15

Random number generation

Practise 25 exam-style questions on Random number generation for AQA GCSE Computer Science (Paper 1). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with the Study Helper on hand if you get stuck.

  • 25 questions
  • 1–8 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

1 mark

A programmer wants to assign a random even number from 2 to 20 inclusive to the variable n, with every even number in that range possible.

Which pseudo-code statement does this?

Choose your answer.

  • n ← RANDOM_INT(2, 20)
  • n ← RANDOM_INT(1, 10) * 2
  • n ← RANDOM_INT(1, 20) * 2
  • n ← RANDOM_INT(2, 20) DIV 2

Model answer

n ← RANDOM_INT(1, 10) * 2

Explanation

RANDOM_INT(1, 10) gives a whole number from 1 to 10, and doubling it gives 2, 4, 6 and so on up to 20, so every result is even and every even number in the range is possible. RANDOM_INT(2, 20) can give odd numbers such as 3, RANDOM_INT(1, 20) * 2 can give numbers up to 40, and RANDOM_INT(2, 20) DIV 2 gives numbers from 1 to 10, including odd ones.

Mark scheme
  1. 1 mark

    n ← RANDOM_INT(1, 10) * 2

    Mark is for AO2 (apply). If more than one option is given, 0 marks.

Question 2

2 marks

The algorithm below simulates flipping a coin five times. A value of 1 represents heads and 0 represents tails.

heads ← 0
FOR flip ← 1 TO 5
  coin ← RANDOM_INT(0, 1)
  IF coin = 1 THEN
    heads ← heads + 1
  ENDIF
ENDFOR
OUTPUT 'Heads: ' + INT_TO_STRING(heads)
OUTPUT 'Tails: ' + INT_TO_STRING(5 - heads)

The calls to RANDOM_INT return 1, 0, 0, 1, 1 in that order.

State the output of the algorithm.

Model answer

Heads: 3
Tails: 2

Explanation

RANDOM_INT(0, 1) can only return 0 or 1, and the loop calls it five times. Three of the values are 1, so heads is 3, and the number of tails is worked out as 5 - 3 = 2 rather than counted separately. Both lines are output once, after the loop.

Mark scheme
  1. 1 mark

    Heads: 3

    Both marks are for AO2 (apply). I. Quotation marks.

  2. 1 mark

    Tails: 2

    R. if either line is output more than once. I. Quotation marks.

Question 3

2 marks

A quiz program picks each question at random from a list of 20 questions, using RANDOM_INT(1, 20) to choose the question number. Players have noticed that the same question is sometimes asked twice in one quiz.

Explain why this happens, and describe how the program could be changed so that a question is never repeated in the same quiz.

Model answer

Each call to RANDOM_INT(1, 20) is independent and can return any number from 1 to 20, including one that has already come up, so the same question can be chosen again.
The program could store the number of each question asked in an array. After generating a number, it checks the array: if the number is already there, it generates another one and checks again, repeating until it finds a number that has not been used, then adds that number to the array.

Explanation

Random number generators do not keep track of earlier results, so repeats are always possible. Preventing them needs extra logic: remembering what has been used and rejecting it, or removing used questions so they cannot be picked. Simply generating the number again once is not enough, because the second number could also be a repeat.

Mark scheme
  1. 1 mark

    Each call to RANDOM_INT can return any number in the range, including one that has already been generated

    Mark is for AO2 (apply). A. the random numbers do not depend on / remember earlier ones. NE. 'it is random'.

  2. 1 mark

    Store the numbers of the questions already asked (e.g. in an array) and, if a new random number has already been used, generate another one until an unused number is found // remove each question from the list once it has been asked and choose only from the questions that are left

    Mark is for AO3 (design). A. shuffle the questions once and ask them in order. NE. 'check whether it has been asked' with no way of remembering which questions have been asked.

Question 4

5 marks

The algorithm below simulates a player moving along a board.

01position ← 0
02turns ← 0
03WHILE position < 15
04  roll ← RANDOM_INT(1, 6)
05  position ← position + roll
06  IF position = 6 THEN
07    position ← 11
08  ELSE IF position = 13 THEN
09    position ← 3
10  ENDIF
11  turns ← turns + 1
12ENDWHILE
13OUTPUT turns

The calls to RANDOM_INT return 2, 4, 2, 6, 6 in that order.

Complete the trace table shown in the figure for the algorithm. The first row has been completed for you.

Give your answer as the values in each column, in order.

Trace table with four columns: roll, position, turns and OUTPUT. Row 1: position = 0, turns = 0; roll and OUTPUT blank. Rows 2 to 11 are empty, for the student to complete.

Model answer

roll: 2, 4, 2, 6, 6
position: 2, 6, 11, 13, 3, 9, 15
turns: 1, 2, 3, 4, 5
OUTPUT: 5

Explanation

Each turn adds the roll to position and then checks the two special squares: landing on 6 moves the player up to 11, and landing on 13 sends them back to 3. After the fifth turn position is exactly 15, so the condition position < 15 is false and 5 is output. Common mistakes are missing the move back to 3 after landing on 13 and outputting turns inside the loop.

Mark scheme
  1. 1 mark

    roll column: 2, 4, 2, 6, 6

    All marks are for AO2 (apply). Values must be in this order. I. Repeated values.

  2. 1 mark

    position column: 2, 6, 11

    Ignore the initial 0 that is already printed. Both 6 and 11 are needed.

  3. 1 mark

    position column: 13, 3

    Must follow 2, 6, 11.

  4. 1 mark

    position column: 9, 15

    Must follow 13, 3.

  5. 1 mark

    turns column: 1, 2, 3, 4, 5 and OUTPUT column: 5, output once after the loop

    R. if more than one value is output. I. Quotation marks.

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

Study Helper Hints first, answers after you try

Stuck on this one? Ask for a hint and I’ll point you in the right direction, without giving the answer away. Once you’ve checked your answer, I’ll show you how the marks are awarded.