Udemy

Random Numbers in Arduino

A free video tutorial from Shawn Hymel
Engineering Superhero
Rating: 4.7 out of 5Instructor rating
1 course
22,283 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 February 2023

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 serial terminal as soon as the program starts in setup write Serial.begin 9600 under that write for int i equals zero semicolon i less than five semicolon i plus plus. Close that out. Open curly brace under that serial.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.println. Open Parentheses. Close parentheses. We're using the built in Arduino 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 1 and 100, which means the minimum is one and the maximum value is one less than the second argument. Run it. 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 Arduino.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 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 ten close parentheses. Just before serial.begin upload and look at the output. Aha. Different numbers. Okay, let's reset the Arduino blast. It's the same set of different numbers over and over and over again. The seed changed 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 5V, but we have no clue what that value might be. That sounds pretty random. So we replace ten with analog read a zero. This reads the analog voltage of pin a zero on the Arduino and returns a number between zero for zero volts and 1023 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 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 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 seeding needs.