Random Numbers in Arduino

A free video tutorial from Shawn Hymel
Engineering Superhero
Rating: 4.8 out of 5Instructor rating
1 course
18,369 students
Random Numbers

Learn more from the full course

Arduino Programming and Hardware Fundamentals with Hackster

Learn electrical engineering basics to build circuits and program Arduino to make wearables, robots, and IoT devices

06:47:50 of on-demand video • Updated March 2022

Master the fundamentals of Arduino programming with C/C++
Build functioning circuits on a breadboard
Control sensors, robots, and Internet of Things (IoT) devices using Arduino
Write programs that perform basic math, light up LEDs, and control motors
Design circuits and write code for your own project
English [Auto]
Sometimes you don't want to have to wait for user input, and sometimes you just don't want things to be preordained if you want chaos in your program. There's good news. You can generate a random number, but it takes some effort to set up. Let's print five random numbers to the cereal terminal as soon as the program starts in set right, cereal begin. Ninety six hundred under that right for int i equals zero semicolon I less than five semicolon I plus plus close that out. Open curly brace under that cereal print random open parentheses one comma one or one close parentheses and then serial dot print. Quotes, space quotes, and make sure you've closed out that for loop under that serial print, L.N. open parentheses, close parentheses we're using the built in are doing a random function to make this happen. We feed random two integers as arguments random will randomly pick an integer between these two integers, including the first, but excluding the second. In our example, random will return something between one and one hundred, which means the minimum is one and the maximum value is one less than the second argument runit. We see five seemingly random numbers between one and one hundred printed to the terminal. Why do I say seemingly? Well, let me press the reset button on my arduino. What happens? Those are the same numbers I can keep pressing the reset button and the same supposedly random numbers will keep appearing. What's going on here? To find out, we turn to our trusty reference guide on Arduino. Scroll down to random numbers and click on Random. This says that the numbers are pseudo random. Pseudo random means that the numbers satisfy one or more statistical tests for randomness, but produced by a definite mathematical procedure. This means that the numbers in our sequence, when compared to each other, are random. However, the particular algorithm used to generate these numbers will produce the same sequence every time. The way to avoid this is to set the random number generator seed. This is just another number used to initialize the generator to do this call. Random seed open parentheses 10 close parentheses just before serial biggin upload and look at the output ahat. Different numbers. OK, let's reset the Arduino blast. It's the same set of different numbers over and over and over again. The speed change, the pseudo random number sequence, but having a constant seed causes the same set of numbers to appear again. We need to find a way to change the seed every time we start the program. Can you think of anything that might be different every time we cycle power on the Arduino? This isn't an easy problem in order to generate truly random numbers, we need a truly random seed. One trick we can do in the Arduino is read the voltage from an analog pin that we're not using. If the pin isn't attached to anything, we can consider it floating, which means the voltage is unknown due to the internal workings of the Arduino. It's likely to be somewhere between zero and five volts, but we have no clue what that value might be. That sounds pretty random. So we replace 10 with analog read a zero. This reads the analog voltage of Pinay a zero on the Arduino and returns a number between zero four zero volts and one thousand twenty three for five volts. We'll talk more in detail about reading analog voltages in a later lecture. But for now, know that this works well for seeding your random number generator. Click, upload and look at your cereal monitor. Now, when I reset the arduino, you'll see a much more random set appear every time. Remember, this trick only works if you have nothing connected to a zero driving the voltage to any particular value. If you don't have any analog ports available, there are some other tricks you can do to get a random seed. For example, you might count the time it takes for somebody to push a button. As it turns out, humans aren't that precise. So timing, user input is a pretty random thing to do for now. Know that using analog read should get you through most of your random seating needs.