Can Arduino Generate Random Numbers? (Random Function Guide)

As Heracitus once said, “The only constant in life is change”. That’s very true as we all wake up to a new day without knowing what exactly to expect. Of course, you can plan out your day, but sometimes, plans can fail. Personally, I love unpredictability.

Consequently, when I started thinking of a project I can do with my Arduino, I decided I wanted to add an element of surprise into it. In order to do that, I needed to find a way to add this feature in my code, so I started researching:

“Can Arduino generate random numbers?”

The Arduino does have the ability to generate random numbers. In fact, it conveniently has two different functions the programmer can use in the Arduino IDE: the random() function and the randomSeed() function.

Now, you may be scratching your head and wondering why there are two different ways to generate random numbers (at least that’s what I did when I first came upon these functions). Wouldn’t that be redundant? Well, even though they have a common purpose of creating random numbers, they do have their differences that are advantageous in certain circumstances.

As a result, the rest of this article will dissect what these differences are.

Overview Of random() Function

The random() function is relatively simple to use. You can store it in a variable or use it directly. Here’s the syntax and an example:

// Syntax
random(min, max); // the min is optional

random(max);

// Example
long randonNumber = random(5);

As you can see, there are two ways to use the random function. You can choose to input a minimum, but you have to include a maximum number. If you don’t input a minimum number, the default minimum value would then be zero.

Moreover, the minimum value is inclusive whereas the maximum value is exclusive. That means that the random number generator could output a number involving the minimum value, but the maximum value wouldn’t be included.

Additionally, the random number the random() function outputs is known as a pseudo-random number. This means that this isn’t a truly random number. Instead, when you use the random() function, the computer actually uses an algorithm to output a number that appears to be random.

Still, even if you had a truly random number generator, the output wouldn’t look too different from each, so this method of obtaining a random number is acceptable.

The only downside to this is that if you reran your random() function, you would still get the same set of “random” numbers.

For example, say you got the following numbers on your first iteration:

1
5
7
3
2
4
7
5
6
8
0
9

Then, your second set of random numbers would be the same as the first:

1
5
7
3
2
4
7
5
6
8
0
9

You can clearly see here that the computer is using an algorithm to get these seemingly random numbers.

When I first started getting into C++ and using its random function, I was surprised to encounter this because my experience with other programming languages didn’t involve the random function repeating the same set of numbers for each iteration.

However, you don’t need to fear. You can easily resolve this issue by using the randomSeed() function.

Overview Of randomSeed() Function

In addition to the random() function, there’s another random number function called randomSeed(). Obviously, this also generates a random set of number that’s the same from one set to another, but the part where it differs from the random() function is that it can actually generate a new set of random numbers in a different iteration rather than repeating the sequence.

Anyways, here’s the syntax for using randomSeed() and an example to go along with it:

// Syntax
randomSeed(seed);
// the 'seed' parameter can be any number and it basically just starts the process for generating a long random sequence of numbers

// Example
unsigned long randomNumbers = randomSeed( analogRead(0) );

As you can see in my example, I inputted ‘analogRead(0)’ instead of a straightforward number like 39. Consequently, you may be wondering why I did that.

The answer is that I wanted each new sequence (or new set) of random numbers to be truly random rather than have matching sets of numbers.

For instance, if I had just done randomSeed(49), it would have given me a random set of numbers. However, the next set of numbers would be the exact random numbers as the first set.

By using analogRead(0) as my seed for randomSeed(), I can generate different sets of random numbers even if I have nothing connected to the A0 pin on my Arduino board. That’s because this analog pin would just read any kind of noise and return a number between 0 to 1023.

If you are having trouble with your code, then you must read my guide on debugging in the Arduino IDE. For those who don’t know, debugging is fixing mistakes in your code. There’s a lot of great tips I’ve gleamed from my own experience!

Summary – tl;dr (Too Long; Didn’t Read)

Here’s what you should take away after reading this article:

  • Arduinos can generate randoms numbers if you use the random() function or the randomSeed() function.
    • Generating random numbers can be fun and useful for Arduino projects involving surprises like “rolling a dice”.
  • These two functions can both generate a sequence of pseudo-random numbers
    • Pseudo-random numbers are numbers that are generated by an algorithm instead of being truly random.
  • They’re also similar in that each time you run the function, the sequence of random numbers you get for each set is going to be identical.
  • The part where they differ is that randomSeed() will allow you to generate a random sequence of number for each set/iteration as long as you put ‘analogRead(0)’ as its parameter instead of an explicit number.
    • This works even if you didn’t connect anything to your A0 pin because the A0 pin will read some ‘noise’ and just randomly spit out a number between 0 to 1023, which you can then use in your randomSeed() function to generate new, unique sets of random numbers.
  • They also differ in their syntax.
    • The random() function must take a maximum value for its parameter (with the option of including a minimum value if you want).
    • On the other hand, randomSeed() just takes a ‘seed’ for its parameter, which is just a number to get the random number generating process started.

If you want to get better at programming with your Arduino, consider reading my article on the best way to program in the Arduino IDE. I go through step-by-step on the most important steps you need to follow to maximize your experience.

In the end, I hope you enjoyed my article and that it didn’t contain too much random information.

Similar Posts