teach-ict.com logo

THE education site for computer science and ICT

Random numbers

1. Introduction

Random numbers are widely used in computer programming and engineering. So it is important that you know how to create them in the computer language of your choice.

Here are some examples that use random numbers

  • The amount of damage that a character hits you in a computer game
  • Picking a lottery ticket (we shall use this in this section)
  • Creating a random set of values for testing code
  • 'Monte Carlo' testing. Where a simulation is run with a random starting conditions each time - weather forecasters use this to see how accurate their forecast is likely to be as the more consistent the result, then the more likely the simulation is accurate.
  • Encryption - creating massive random keys to scramble data
  • Creating variety and surprise in game programming

We are going to use Python 3 to show how random numbers are produced. A number of Python demo files are included in page 10 so you can run the code on your own computer.

In your exam, the OCR exam reference language format will use this format to form a random number:

    random( ..., ....) 
for example, to return a random number in the set 1 to 6, the command is

    random (1,6)