How to Generate Random Numbers using Arduino


In this tutorial, we will learn how to generate random numbers by using Arduino.

You may see many times a captcha verification of a random number and also some games.

We generate random numbers that have a range from zero up to a specified maximum number or between a minimum and maximum value you provide.

The random function is used to return a random number. Calling random() with a single parameter sets the upper bound. And these values will range from zero to one less than the upper bound:

random(max);         //return a random number between 0 to max-1

Calling random() with two parameters sets the lower and upper bounds. This values will range from the lower bound to one less than upper bound:

random(min, max);    //return a random number between min and max-1

Code for random numbers

Here is an example that uses the different forms of random number generation available on Arduino. Upload the below code on Arduino.


output random numbers

If you press the reset button on Arduino to restart the sketch, the first lines of random numbers will be unchanged.

Only the last line changes each time the sketch starts because it sets the seed to a different value by reading it from an unconnected analog input port as a seed to the randomSeed function.

If you are using analog port 0 for something else, change the argument to analogRead to an unused analog port.

Here some more tutorials for you:

Our Youtube Channel


Please enter your comment!
Please enter your name here