Login

Back

An algorithm for generating a list of numbers

When we are writing code, we often need some data available so we can test what we have written. We can type into our program a set of data to use manually or we could make a list of data and save it on a file and then use that, but we might have to type in thousands of pieces of data. And we might have to run the same test again but with a different set of data! Clearly, we don't want to be typing in lots of data if we can find a way to generate some automatically.

An algorithm is simply a very precise description of how to solve a problem. It may be a diagram (like a flowchart) or it may be a description in the form of a set of phrases or sentences. If we can also write those sentences in the style of a programming language, then we have something called 'pseudo-code'. Pseudo-code is really good because it is relatively easy to convert it into any programming language we want. For this reason, we say pseudo-code is 'language-independent'. We are going to write an algorithm to produce a set of numbers. We will also include instructions to print out the list, so we can test it works.

Import a random number generator.
Create an empty list, one with no elements (pieces of data) in it.
Ask the user how many elements they want in their list
For each position in the list:
      generate a random number and add it on the end of the existing list

for each position in the list:
      print out what is in that position

1. Most programming languages have a box of tools for generating random numbers. These are pre-written, tested and working functions that we can just use. We start our algorithm by importing these tools.
2. We then create a data structure to hold our data. To start with it will be empty. All languages have data structures and most have one that can be use called a list or an array. It is simply a container that holds data items in positions, usually starting at position 1, then position 2, then position 3 and so on. Each position is called an 'element' and can hold a piece of data.
3. Next, we need to ask the user how big they want their list, how many elements they want in it. 
4. Then, we start at the first element, generate a random number and add it to the list, and we do this for every element up to the size that the user wanted.

This fills an array with data items! To print it, so we can see what happened, we do the following:

1. We go to each position in the list in turn, up to the end of the array
2. We print out what we find there.

So that's it. We have an algorithm that precisely describes how to generate a list of data we can use for testing, and how to print it out. As we have said, algorithms written like this are called 'pseudo-code'. What we have written isn't a program we can run, but we can give it to a programmer in any language and they should be able to convert the algorithm into the language that they are skilled in writing.

In  a sense, it is a little bit like writing a Maths formula to do something e.g. A = πr2 and then teachers of different nationalities all over the world can use their own languages to explain to their students what this means and how it works. The Maths formula is the algorithm and the teachers then use their own languages to explain it. 

Back