Random Numbers in Arduino

Shawn Hymel
A free video tutorial from Shawn Hymel
Engineering Superhero
4.6 instructor rating • 1 course • 10,548 students

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 February 2020

  • 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 5 random numbers to the serial terminal as soon as the program starts in setup. Right. Serial dock begin. Ninety six hundred under that right for it. Equals zero semi-colon. I less than five in my colon I plus plus close that out open curly brace under that serial dat print random open parentheses one comma 101 close parentheses and then serial dot print quotes space quotes and make sure you've closed out that for loop under that right serial dot print. L.N. open parentheses close parentheses. We're using the built in Arduino random function to make this happen. We feed random to 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 1 and 100 which means the minimum is 1 and the maximum value is one less than the second argument running we see five seemingly random numbers between 1 and 100 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 arguido dot CC. 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 and or 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 closed parentheses just before serial does begin. Upload and look at the output. A ha. Different numbers. OK let's reset the Arduino blast. It's the same set of different numbers over and over and over again. The seed 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 0 and 5 volts but we have no clue what that value might be. That sounds pretty random. So we replaced 10 with analog read a zero. This reads the analog voltage of pinned a zero on the Arduino and returns a number between 0 for 0 volts and 1023 for 5 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 serial 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 0. 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 seeding needs.