CS24 : That's a bit random!

One of the most interesting feature of computers is the immense power they have to model real world events. The real world is inherently random - hence, computers need to be able to simulate this. Random number generation is only covered in certain specifications, however, even if it's not, there is no harm in covering this section.

We are learning ...
  • How computers generate random numbers
  • How they can be used to model real world situations
So that we can ...
  • Understand how random numbers are generated
  • Use random number generation in real world modelling situations.


Activity 1 Random number generation (100)

Generally, random numbers are generated between 0 and 1 and are generated by hardware or software Random Number Generators (RNG).

Surprisingly, there are two different 'types' of RNG ...
  • Pseudo-random number generators (PRNG)
  • True random number generators (TRNG)
Leave a machine alone to generate random numbers and you may eventually detect a pattern in the number due to the way in which the numbers are calculated. Expose the machine to a truly random outside influence like radioactive decay or weather patterns and true randomness results.

Click image to enlarge

Random Numbers - Numberphile (11:38)

Random Numbers (the next bit) - Numberphile (9:22)

Pseudorandom number generators (6:40)

Task 1.1

Let's use Pythons PRNG (pseudo random number generator) to generate a sequence of random numbers ...

  • Open up the Python programming environment, IDLE.

  • I used Pythons PRNG to generate a series of 5 random number between 0 and 1. The results of my efforts are shown in the screenshot below. I've seeded the RNG with a value from which it will base it's random number calculations.

  • Now you try! Make sure you use the same seed value! What do you notice? Compare this to the same exercise carried out by your peers on different computers. Still the same?

  • In your notebooks : Write about what you have found out. Include screenshots to illustrate your point.

    The pseudorandom number generator built into Python uses a seed value to generate a random sequence of patterns. Normally, you wouldn't supply the seed value because you end up with a chance of getting the same 'random' sequence!

    Instead, the PRNG uses the
    system time as the seed which, obviously, will be different every time the generator is used - unless two people generated the random number at exactly the same time ...

  • At the prompt : Try out some of the random number generation functions. Before you start, you have to ...

    >>> import random

    ... because there are no random number functions in the core Python library. I've tried to explain what the following random functions do. Your job is to produce a practical example for each one and document what you have done.

    Generate a random float value within the range 0.0 to 1.0 inclusive.

    Generate a random integer between optional start and stop with optional parameter step. It will never produce the value of stop because it is based on the operation of the range() function. So the actual range is start through stop-1. Does not require the start parameter but cannot have step unless it has start (obviously). Useful for generating a random index for a list for instance.

    Generate a random integer between start and stop, inclusive. Does not support the step parameter.

    Great function which picks a random value from sequence.

    Shuffle the sequence in place. The sequence must be a mutable object like a list.

    In your notebooks : Document examples for each of these random functions.

OUTCOME : Is random, truly random? Your assessment, please. Plus, many randomisations and examples therein.

There are plenty of resource online about random number generation, especially the connection between pseudorandom numbers and true random numbers. Spend a little time meandering around the random.org website before you attempt the following task.

Click to visit website and spend a little ...

... time.

Task 1.2
 Making pseudo true?

If you haven't done so already, read through the section of www.random.org called 'Introduction to randomness'. Don't give me none of this ...


... just read it, because :

In your notebook : Make some notes on the methods used to simulate TRNG on computers. Don't just write paragraphs of text - try to use colour and diagrams if you can.

OUTCOME : Explanation of how www.random.org generates true random numbers.

Activity 2 Uses of random number generation (40)

Random number generation is used extensively in all areas of life, including computational modelling.

Task 2.1 
Dice throwing simulation

Download diceThrow.py from the lesson resources and save it in a suitable place in your documents. This is a simple program which demonstrates how to choose a random value from a list but it's written using structured programming techniques.

In your notebooks : Print out the script using Notepad++ to syntax highlight it and make it look nicer. Stick it in your books. Then draw a structure diagram (see 'Break it down' if you've forgotten what this is).

OUTCOME : Structure diagram for dice throwing simulation.

Focus on computational modelling

Computer simulations involve the contruction of models using a computer program which represent real or imaginary situations. They allow the user to study or try things that would be difficult or impossible to do in real life and often involve the use use Queues (a data structure we have met briefly before). Computer simulations normally deal with a system which changes over time and always involve some element of randomness in their operation.

One of the earliest computational models ever developed was Conways Game of Life.

Conways Game of Life (3:29)

Task 2.2 

There is a great, free piece of software called Golly which you can use to investigate Conways Game of Life. Visit the Golly homepage and download the software.

On your computer : Spend a little time investigating the operation of Golly. There is no need to document what you have found out, but maybe, it will inspire you ...

OUTCOME : No particular outcome, just experience.

Extension Activities 

How about these?
  • There is plenty more about the Python random number generator at the Python random reference.

  • Truly Random by David Harris. Nice.

  • Watch, listen and download random numbers at the Australian National University Quantum Random Number Server.

  • There is a copy of the mindmap from Task 2.1 in the lesson resources. Download it and complete it using Freeplane. Print this out and stick it in your notebooks.

  • The Meteorological Office uses supercomputers to run very complex simulations of the atmosphere in order to be able to forecast the weather accurately. You could start by gathering weather forecasts from national newspapers, and so on, to focus on what forecasting is already done; alternatively, you could watch weather reports on television. Perform some research and produce a report on how supercomputers are used in modelling complex systems such as the weather.

  • Download free traffic simulation software from SwissEduc.

  • There is another simulation in the resources called 'Ant Simulator'. Download the zip file, extract it and double click LocasAnts.exe to start the simulation.

What's next?

Before you hand your book in for checking, make sure you have completed all the work required and that your book is tidy and organised. Your book will be checked to make sure it is complete and you will be given a spicy grade for effort.