levo

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.

Your recallNot started
Process

What is the effect of seeding a generator with the same value twice, and when is that useful?

Evaluation

Explain the advantages of handling exceptions rather than letting the program stop.

Questions from this subtopic

Each one with the model answer underneath.

  1. 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. 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. 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. 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. 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. 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.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

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.