Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
OCR GCSE Computer Science (Paper 2) Smart practice A mixed round aimed at your weak spots
Unit 1
Algorithms
Unit 2
Programming fundamentals
Unit 3
Producing robust programs
Unit 4
Boolean logic
Unit 5
Programming languages and IDEs
Unit 6
Practical programming (Section B)
Avg
score:
–
Topics
done:
0/31

Random number generation

Practise 22 exam-style questions on Random number generation for OCR GCSE Computer Science (Paper 2). 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.

  • 22 questions
  • 1–7 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

Which of these values could NOT be returned by random(-1.0,10.0)?

Choose your answer.

  • -1.0
  • 0.5
  • 9.99
  • 10.5

Model answer

10.5

Explanation

random(-1.0,10.0) gives a real number from -1.0 to 10.0, including both limits, so -1.0, 0.5 and 9.99 are all possible. 10.5 is larger than the upper limit. A common mistake is to think a negative value or the lower limit itself cannot be returned.

Mark scheme
  1. 1 mark

    10.5

    Only this option: it is above the upper limit of 10.0. -1.0 is the lower limit, which is included. If more than one option is given, 0 marks.

Question 2

2 marks

A game places treasure in a random square of a grid with 10 rows and 10 columns. The rows and the columns are both numbered from 0 to 9.

Write two statements, using OCR Exam Reference Language or a high-level programming language, that store a random row number in row and a random column number in col.

Model answer

row = random(0,9)
col = random(0,9)

Explanation

The rows and columns are numbered 0 to 9, and OCR's random includes both limits, so random(0,9) gives every possible row or column. Two separate calls are needed so that the row and column are chosen independently; using one value for both would only ever place treasure on the diagonal.

Mark scheme
  1. 1 mark

    row = random(0,9)

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors. Accept random.randint(0, 9) (Python). Do not accept random(1,10) or random(0,10).

  2. 1 mark

    col = random(0,9)

    Must be a separate call to random, stored in col.

Question 3

3 marks

A card game program uses this code.

suit = random(1,4)
switch suit:
    case 1:
        print("Hearts")
    case 2:
        print("Diamonds")
    case 3:
        print("Clubs")
    default:
        print("Spades")
endswitch

State the output when random returns 2 and the output when random returns 4. Explain why each of the four suits is equally likely to be output.

Model answer

When random returns 2: Diamonds
When random returns 4: Spades
random(1,4) gives 1, 2, 3 or 4, each equally likely. Each value matches exactly one suit - 1, 2 and 3 have their own case and 4 is the only value left for default - so each suit has a 1 in 4 chance.

Explanation

The switch compares suit with each case in turn; 2 matches case 2, and 4 matches no case, so the default branch outputs Spades. Since random(1,4) can only return 1 to 4, the default is reached only by 4. If the code had used random(1,5), Spades would be output for both 4 and 5 and would be twice as likely as each other suit.

Mark scheme
  1. 1 mark

    2: Diamonds

  2. 1 mark

    4: Spades

    4 matches no case, so the default runs.

  3. 1 mark

    random(1,4) gives each of 1, 2, 3 and 4 with equal chance, and each value leads to exactly one suit (only 4 reaches the default)

    Must link the four equally likely values to the four suits.

Question 4

5 marks

A student wants to check whether a simulated dice is fair.

Write a program, using OCR Exam Reference Language or a high-level programming language, that simulates rolling a six-sided dice 600 times, counts how many times a 6 is rolled, and outputs that count.

Model answer

sixes = 0
for i = 1 to 600
    roll = random(1,6)
    if roll == 6 then
        sixes = sixes + 1
    endif
next i
print(sixes)

Explanation

A count-controlled loop runs the simulation 600 times, generating a new random roll on each pass and counting the 6s. For a fair dice about one roll in six is a 6, so the output should be close to 100, but it will vary from run to run. Common errors are generating the roll once before the loop (so every roll is the same) and a loop that runs 599 or 601 times.

Mark scheme
  1. 1 mark

    sets a counter to 0 before the loop

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors.

  2. 1 mark

    uses a loop that repeats exactly 600 times

    e.g. for i = 1 to 600; Python for i in range(600). Do not accept for i = 0 to 600 (601 times) or range(1, 600) (599 times).

  3. 1 mark

    generates random(1,6) inside the loop

    Accept random.randint(1, 6) (Python). A new number must be generated on every pass.

  4. 1 mark

    checks whether the roll is 6 and adds 1 to the counter if it is

  5. 1 mark

    outputs the count after the loop

    Must be output once, not inside the loop.

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.