random numbers

Even though languages have ways of generating numbers, these numbers are not truly random! A more accurate description of what has been generated is a pseudo random number! The very nature of computers and software is such that random behaviour is not a feature that they have. Because we want them to perform set tasks, by design we have eliminated randomness.

We have ways that we have developed to generate ‘random’ numbers, such as randomSeed() but these are still at the mercy of the algorithm that makes it work! One solution is to introduce a seed, to make it seem random but this still follows a pattern! Depending on the method, it may be complex enough to make it difficult to identify a pattern but it is still following a repeated and consistent pattern each time. This is therefore not truly random. This is still a pseudo random number. If we generate a pattern of random numbers then there should be no way to reverse engineer this and predict the next series of numbers.


Let’s start by looking at randomSeed(). randomSeed() can return a pseudo random, integral (whole) number. When run, the randomSeed() function initialises the random number generator, beginning at an arbitrary point in its random sequence. Random sequence is a key point here! Although the sequence is very long, it is always the same! What we need to do is to introduce a random seed.

All of this however does not mean that you cannot generate truly random numbers. One method is to use noise. This could be noise in any form; electrical, thermal, electromagnetic etc. These can be used as a seed and generate truly random numbers! You may be wondering how this would be possible and how you could feed this into your Arduino! We can use part of our Arduino to an advantage, by using an unsused analog sensor pin!


The first thing that we do is to create a variable, with the data type of long.

long randNum;

In our setup() we begin the serial monitor with a baud rate of 9600. We then set our random seed by reading analog pin 0.

void setup() {


void loop() {
  // print a random number from 0 to 299
  randNumber = random(300);

Sample of pseudo random numbers