Home ArduinoBasics Arduino Basics : randomness

Arduino Basics : randomness

by shedboy71

A random number generation is very important in computing devices which helps them to do the task in random manner.

There are many different times you may need to generate a random number such as a random password generator, in a game like the Magic 8 Ball

There are built-in functions in the Arduino IDE which are designed to help in generating random numbers. These 2 functions are randomSeed() and random() and the details of these are discussed below;

randomSeed()

randomSeed() initializes the pseudo-random number generator, causing it to start at an arbitrary point in its random sequence. This function has a parameter which decides from which point in the sequence should the random number generation starts. To initialize the pseudo-random number generator with a new sequence each and every time, the value provided as the parameter should also be different.

This particular code reads a random value from the analog input pin A0 which is left unconnected so that each and every time the code runs it can generate a different set of random numbers.

random()

The function random() is used to generate a pseudo-random number which falls in a specified range. The function is always called after calling the randomSeed() function. This function has two parameters of which the first one is the lowest required value and the second one is the largest required value

 

It’s important to understand that when you use the random() function with Arduino is that it will generate the exact same list of pseudo-random numbers every time. So it is not truly random. Now you can use the randomSeed() function.

This function takes a value (an integer for example) and uses the number to alter the random list generated by the random() function.

The number you pass to the randomSeed() function is called a ‘seed’. So you can call this like randomSeed(20);

But there is still an issue – even though the sequence of random numbers is different when using the randomSeed() function – it will still be the same every time the sketch is run. Let’s look at this example where use 20 as the seed value

 

long randomNumber;

void setup()
{
  //setup serial communications through the USB
  Serial.begin(9600);
  randomSeed(20);       
}

void loop() 
{
  //generate a random number
  randomNumber = random(1,9);
  //display the random number on the serial monitor
  Serial.print("The Random Number is = ");
  Serial.println(randomNumber);
  delay(1000);
}

Now let’s look at 3 runs of this in the serial monitor and you will see a problem

The Random Number is = 5
The Random Number is = 7
The Random Number is = 4

The Random Number is = 5
The Random Number is = 7
The Random Number is = 4

The Random Number is = 5
The Random Number is = 7
The Random Number is = 4

The solution or an easy solution is to seed the analogSeed function with a value from one of the analog input pins. In theory, this should be a different value although it’s a fairly limited range. For basic projects, it will be perfectly adequate.

 

long randomNumber;

void setup()
{
  //setup serial communications through the USB
  Serial.begin(9600);
  randomSeed(analogRead(5));       
}

void loop() 
{
  //generate a random number
  randomNumber = random(1,9);
  //display the random number on the serial monitor
  Serial.print("The Random Number is = ");
  Serial.println(randomNumber);
  delay(1000);
}

Now run this again and this is what I saw in the serial monitor

The Random Number is = 4
The Random Number is = 7
The Random Number is = 8

The Random Number is = 4
The Random Number is = 2
The Random Number is = 2

The Random Number is = 6
The Random Number is = 8
The Random Number is = 8

The Random Number is = 7
The Random Number is = 3
The Random Number is = 6

The Random Number is = 3
The Random Number is = 1
The Random Number is = 7

Magic 8 Ball example

The Magic 8 Ball is a toy used for fortune-telling or seeking advice, developed in the 1950s and manufactured by Mattel. It is often used in fiction, often for humor related to its giving very accurate, very inaccurate, or otherwise statistically improbable answers.

Now this looks better with an LCD atatched to an Arduino but this one will simply display a random message every 2 seconds from the array of available messages

String phrases[] = {"It is certain", "Certainly", "Without a doubt", "You may rely on it", "As I see it, yes", "Most likely", "Outlook good", "Yes", "My reply is no", "Very doubtful", "No", "No chance", "No way", "Cannot predictt",
"I doubt it", "Ask again", "Not sure"};
String output;
int numberofphrases = 17;  //match this with the number of phrases
 
void setup() 
{
  Serial.begin(9600);
  randomSeed(analogRead(5)); 
}
 
void loop() 
{
  output = phrases[random(numberofphrases)]; //Chooses phrase
  Serial.println(output);
  delay(2000);
}

Open the serial monitor and you should see something like this

No way
I doubt it
Yes
It is certain

Summary

There are more advanced ways of doing this, true random libraries and people have written articles on generating random numbers but for most people the above example will be sufficient

You may also like