Numpy and Simulations


This course has covered:

  • The use of numpy for numerical work in Python
  • Generating random numbers
  • Running simulations
  • Simulating more complex systems with cellular automata
  • Plotting grids of numbers and animating them

Final exercise

Adapt your one-dimensional cellular automaton code to follow a new set of rules. For each cell, look at the cells to its left and right and:

  • if the cell to the left has the same value as the cell to the right, the new value of this cell should be 0;
  • if the cell to the left has a different value to the cell to the right, the new value of this cell should be 1.

Run a simulation for an array of size 100, for 50 time steps. The initial value of the array should be all 0s, except a single 1 at index 50.

Put the code which runs the simulation in a Python script called and run it on the command line. This script should save its results to a numpy output file.

Put the code which visualises the results into a Jupyter Notebook which should read the numpy file and create some graphs or images.

See what happens if instead of starting with a single value of 1, you instead initialise your array randomly.

Note: this automaton has a name: "Rule 90". It's one of a class of automata called elementary cellular automata.


This course was written by Matt Williams. All text is published under a Creative Commons Attribution 4.0 International License with all code snippets licensed as MIT.

The source for the material can be found on GitLab where fixes are welcome.