Arduino

How to Generate the Pseudo-Random Numbers in Arduino

To generate the pseudo-random numbers in Arduino, we use the built-in functions random(),  first, understand why we need to generate the pseudo-random numbers? In real life, we have to generate random codes for verification purposes. Similarly, in digital gaming, we have to generate random passwords. For this purpose, we have to generate the random numbers in Arduino.

In this article, we will discuss the techniques by which we can generate random numbers with the help of examples.

How to generate random numbers in Arduino

There are two built-in functions in Arduino which are used to generate the pseudo-random numbers:

  • random()
  • randomSeed()

We will explain both of these functions in detail.

random(): This function is used to generate the pseudo-random numbers by defining the range of numbers or any specific condition. The general syntax of using the random() function is:

random(max)

According to this syntax, you have to define an integer number, as a result of which it will generate the numbers less than that particular number. To understand the use of this syntax, consider the example:

int randomNumber;void setup(){

Serial.begin(9600);

}

void loop(){

randomNumber=random(500);

Serial.println(randomNumber);

delay(1000);

}


We have declared a variable of int data type globally with the name “randomNumber” in the above code example. In the void loop, using the random() function we stored values in randomNumber and specified the generated numbers should be less than 500. Then with the help of serial communication, we displayed the result on serial monitor output. And in the end, we used the delay function for one second.

The output of the code is:

Every second a random number is being displayed.

The other way of using the random() is specifying the range:

random(min,max);

You have to use the random function with the specified range of pseudo-random numbers by mentioning the maximum and minimum limits. So the numbers generated should be in the specified range, for example, we consider another example:

int randomNumber;void setup(){

Serial.begin(9600);

}

void loop(){

randomNumber=random(100,500);

Serial.println(randomNumber);

delay(1000);

}


The code used in this example is the same as the above example, the only difference is that we specified the range of the random number generated should be between 100 and 500. The output of the code will be:


randomSeed(): The other built-in function to generate the random numbers is by using the randomSeed() function. The random() function generates the pseudo-random numbers according to the range given to it as input, whereas, the randomSeed() function generates the numbers from any arbitrary number. The difference between both the functions is that the output of random() is predictable sometimes whereas it is not possible to predict the output if randomSeed() function is used. The syntax is of randomSeed function is:

randomSeed(seed)

seed : an unsigned long number

Suppose pin 4 is analogue unconnected pin then the function will be used as:

randomSeed(analogueRead(4));

The randomSeed() will generate a different number each time the code is run to ensure the pin is unconnected.

Conclusion

To generate the pseudo-random numbers in Arduino, we can use the built-in functions; random() and randomSeed(), of Arduino. These functions generate random numbers which can be used for various projects of Arduino. In this write-up, we have explained the methods of generating the random numbers in Arduino with the help of examples.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.