AQA A-Level Computer Science 7517
Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation
Fundamentals of Data Representation
Fundamentals of Computer Systems
Fundamentals of Computer Organisation and Architecture
Fundamentals of Communication and Networking
Consequences of Uses of Computing
Systematic Approach to Problem Solving
Fundamentals of Databases
Big Data
Fundamentals of Programming
Random numbers and exception handling
- 2 things to know
- Paper 1
Overview
Recall questions on pseudo-random generators and seeding, scaling a random real into a range, and the structure and purpose of an exception handler.
What you need to know
Answer each one before you reveal it.
Explain the advantages of handling exceptions rather than letting the program stop.
Questions from this subtopic
Each one with the model answer underneath.
- 1
What does a random number generation function give you?
Model answer
A value chosen unpredictably from a range. Languages typically provide a function returning a real value from 0 up to but not including 1, and often one returning an integer between two given bounds; each call gives a fresh value.
- 2
Why are the random numbers a program produces described as pseudo-random?
Model answer
Because they are produced by a deterministic algorithm from a starting value called the seed, not by a genuinely unpredictable physical process. The sequence passes statistical tests for randomness and is unpredictable in practice, but it is entirely determined by the seed and will eventually repeat.
- 3
What is the effect of seeding a generator with the same value twice, and when is that useful?
Model answer
It produces exactly the same sequence of numbers again. That is useful when testing or debugging, because a fault that depends on the random data can be reproduced, and when a simulation must be repeatable so that results can be checked. Programs that want different behaviour on each run seed the generator from something changeable such as the current time.
- 4
A function random() returns a real from 0 up to but not including 1. How do you use it to simulate rolling a six-sided die?
Model answer
Multiply by 6 to get a value from 0 up to but not including 6, truncate to a whole number to get 0 to 5, then add 1: int(random() * 6) + 1. In general, for a whole number from low to high inclusive, use low + int(random() * (high - low + 1)).
- 5
Give three uses of random number generation in programs.
Model answer
Games, for dice, shuffling cards or placing enemies; simulations and modelling, such as queueing or weather models where events occur with a given probability; and generating test data or selecting a random sample from a data set. Standard generators are not suitable for security purposes such as generating keys, because the sequence can be predicted from the seed.
- 6
What is an exception?
Model answer
An error condition detected while the program is running that stops the current statement completing normally, such as dividing by zero, converting non-numeric text to a number, using an index beyond the end of an array, or opening a file that is not there. If nothing deals with it the program halts with an error message.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Fundamentals of Programming, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.