Practise Random number generation. 12 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.
Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.
random(a, b) returns a random whole number from a to b inclusive. State all the possible values of random(1, 6).
1, 2, 3, 4, 5, 6
List all the possible values of random(3, 5), from smallest to largest.
3, 4, 5
Sample questions
Written for this site in the style of OCR exam questions. They are not taken from real past papers.
Question 1Easy3 marks
random(a, b) returns a random whole number from a to b inclusive.
(a) State all the possible values of random(1, 6).[1]
(b) Give one example of a program that would use random numbers.[1]
(c) Which line of code simulates rolling a normal six-sided dice? Tick (✓) one box.[1]
roll = random(0, 6)
roll = random(1, 6)
roll = random(1, 7)
roll = random(0, 5)
Show the answer and mark scheme
(a)Answer: 1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5 and 6
(b)
any suitable example, e.g. a dice game / choosing quiz questions / a simulation / generating passwords / shuffling cards
(c)Answer:roll = random(1, 6)
Question 2Medium6 marks
random(a, b) returns a random whole number from a to b inclusive.
(a) Write an algorithm that simulates rolling a six-sided dice 100 times and outputs the number of times a 6 was rolled. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[4]
(b) State roughly how many sixes you would expect, and explain why the output may be different each time.[2]
Show the answer and mark scheme
(a)Answer:
sixes = 0
for i = 1 to 100
roll = random(1, 6)
if roll == 6 then
sixes = sixes + 1
endif
next i
print(sixes)
a loop that repeats 100 times
generates random(1, 6) inside the loop
adds 1 to a counter (set to 0 before the loop) when the roll is 6
outputs the counter after the loop
(b)
about 17 (100 ÷ 6)
each roll is random, so the number of sixes varies from run to run
Question 3Hard8 marks
The ASCII codes for the upper-case letters A to Z are 65 to 90. random(a, b) returns a random whole number from a to b inclusive.
(a) Write a function that takes a length as a parameter and returns a random password of that length. Each character must be a random upper-case letter. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[5]
(b) Calculate how many different 3-character passwords the function could produce. Show your working.[2]
(c) Suggest one change to the function that would make its passwords harder to guess.[1]
Show the answer and mark scheme
(a)Answer:
function makePassword(length)
pw = ""
for i = 1 to length
pw = pw + CHR(random(65, 90))
next i
return pw
endfunction
function with a parameter for the length that returns a value
the password starts as an empty string
a loop that repeats length times
generates random(65, 90) and converts it to a character using CHR
joins each character to the password and returns the password after the loop
(b)Answer: 263 = 26 × 26 × 26 = 17 576
26 × 26 × 26 / 263 seen
17 576
(c)
include lower-case letters / digits / symbols (a larger set of characters) / make the passwords longer