Using random numbers with Arduino
Youll need a breadboard and youll need at least one jumper wire. Setting up this circuit is very simple, were going to be using pins, two three and four, so just take one of your resistors put it in, pin two on your Arduino and then hook. The other end into the breadboard and then take the long leg of the light emitting diode of one of the light emitting diodes place it in the breadboard connect it to that resistor and then put the short leg in one of the long ground rails. On the breadboard, and then just do this four pins, 3 amp 4 with the resistors and LEDs and then finally connect a jumper wire from the ground on the Arduino to that ground, rail on the breadboard and thats. It go ahead and plug in your Arduino board. So here we are in New York, we know IDE and what Im gon na do is open up a file here in the examples, and I go to basics and Im just gon na go to bare minimum, then what this gives me is just a void setup And avoid loop nothings in it, but it just kind of sets that up for me a thats, less typing that I have to do so. I like to do that so again, what this is going to be about is random numbers, and what were going to do is set up three LEDs. You know you just saw the circuit and we just want to randomly turn on one of those LEDs to do that.
Lets go first, declare some pin numbers for the LEDs, so we have three LEDs, will call them led one led to and led three and theyre at pins, 2 3 amp 4 respectively. Now lets create a variable that is going to store the random number that we generate were going to make it of datatype long. So why are we declaring the random number variable as a long? Well, the reason were doing. That is because the random function actually returns. A value that can be really big and its stored as a long data type, and so really the only difference between an integer datatype and a long data type is that the long data type can hold a much larger value over, like 2 billion, so thats. Why? We go with the long data type again its gon na allocate more storage for that number. Now, in this case, we really dont, for our purposes were not going to be generating. Some giant random number were going to be generating a number between like 2 amp 4. So we could use an integer, but for our purposes it really doesnt hurt to use the long. So now lets jump into the setup and what were going to do is we want to be able to look at the random numbers that you get that get generated? So lets go ahead and start serial communications and then we also want to set our LED pins as output lets use the pin mode function to do that.
So weve set the mode of LEDs, 1, 2 amp 3 as outputs, and the only other thing I want to do in here is: I want to send a little message and set up that will get displayed on the serial monitor that lets. Us know that when weve started a new sequence of random numbers, so lets do that. So now, when we look at the serial monitor, we will know when a new sequence of random numbers begun begins. Okay lets go into the loop. The first thing were going to do is: assign a random number to our random number variable. So to do this, we just use the random function, so what were doing is were saying random number thats, the variable that we declared up at the top were setting it equal to the output of the random function and were passing the random function. The number five so lets take a quick look at the syntax of the random function on the arduino reference pages. Now, if you know me, you know if youve done any of my courses, you know that I love the Arduino reference Im there all the time, and here we are at one of the reference pages for the random function. So if we look at the syntax, whats cool is that it can take two different parameters, so you can either send it just the max number. So I just sent it the number five or you can send it a minimum and a maxi max number that you send what it actually returns.
If we look down here at the return, it sends you the max minus 1, so that max number is not inclusive. So, in our case, its going to return a random number between 0 and 4 again, because that max number will not be included in the random numbers that could generate it. So its going to send us a number, you know either 0, 1, 2, 3 or 4 is what well get back. So lets go ahead and take a look at the numbers that this function generates and if we want to look at the numbers it generates were going to have to print those numbers to the serial. Monitor. So lets do that so now Im going to go ahead and upload this to the Arduino board, but Im really not at this point interested in looking at the LEDs. What I want to look at is the output on the serial monitor, so lets open up the serial monitor and what Im going to do is reset the Arduino board and Im also going to turn off auto scroll okay. So I reset the Arduino board, and this is what Im interested in so here we started the new sequence of the random number thats one set up occurred, and then here is the sequence of random numbers, and what Im going to do is just take a quick Screenshot of those numbers – okay and now what Im gon na do is Im gon na go ahead and hit reset again.
So I just reset the board and then Im gon na turn off auto scroll and were gon na come back up here and were gon na. Take a look at this random sequence and now Im gon na go ahead and open up that screen, shot that I had okay, so heres the screen shot from the previous time we started, and here is the next time we started. I want you to take lets. Take a look at these at these numbers, so the first one was two. The second one was four three three zero, two four okay. So the point Im trying to make here is that the random numbers that you get generated using the random functions are the exact same from one instance to the next, so its just this long reel of randomly generated numbers. Does that make sense so the same its gon na be the same quote: unquote random numbers over and over again its called a pseudo random number is what it ends up being okay, so this you know, is kind of an issue I mean, if were trying to. If were trying to have real randomness in the program, we dont want the same numbers to repeat over and over and again so. How are we gon na fix that well lets try using the random seed function, all right so random seed? What it does is it initializes, a pseudo random number, so lets see the parameters it takes.
Ours is a long or an integer, so we have to pass a number and this. What that does then is generate a seed. So lets go ahead and do it Im going to do it up here in the set up right at the bottom, so Ive written the random seed number and Ive just kind of come up with an arbitrary integer 42 to put in there to seat it. So lets go ahead and run this and take a look at the serial, monitor and see what it looks like. Okay, so here is the start of a new sequence. Let me get the previous sequence and lets compare it. Okay, so here was the previous sequence: weve got a two thats, a four, this time, heres a four thats, a three hey. This is definitely this new seed is definitely different from when we werent using the random seed, thats cool. What Im going to do is take a quick screenshot of this and then what well do is run the program again and see see if it is indeed, you know, given us some random sequences. Okay, so heres a new sequence lets open up that new screen shot and lets. Compare it: okay. 4. 3. 4. 3. 1. 1. 1. 0. 0. Ok, so we can see. Whats happened is, although the sequence is different from when we werent using random seed, that its the still the exact same from one to the next, so weve generated a new sequence of random pseudo, random numbers, but its the same sequence.
So it doesnt seem like that. Has really helped us all that much so, how are we going to overcome this well lucky for us, its written right into the documentation, so lets go ahead again and check out the arduino reference page for the random seed function. So it says here: if we want to generate different values on subsequent executions of the sketch, then we need to provide some type of fairly random input and what it recommends is using the analog read function on some unconnected, pin so thats. A really neat idea. So lets go ahead and do that so were going to get rid of 42 were going to type in analog read, so we are passing the random seed function, a function in itself and its the analog read function. So if you recall what analogue read does is it looks at one of the analog pins that you specify in this case Im specifying analog, pin a zero, and since we have nothing connected to a zero when it reads a value from there, its semi, you know Its just kind of gon na read any type of noise that might be at that pin and its going to return a number between zero and a thousand and twenty three and, generally speaking with the unconnected pin, you know who knew we just really dont know what Its going to return so lets go ahead and see if this works all right, so weve uploaded.
It lets take a look at the serial monitor. Okay, so heres a start. Heres a random sequence lets open up the previous random and lets see if its different okay. So this was the one we were using the random seed, but were using the number 42. This is the new one. With our analog read, input so were getting four three okay, so this is different: hey thats, a good start lets, go ahead and reset this and then compare if this so Ill. Take a quick screenshot of this and lets see if this will be different from the next time. We run it so Ill, just reset the Arduino board. Okay, so I reset the board. We got some new numbers lets, go ahead and open up that previous sequence. Okay, so we can see from one time to the next. It was definitely different. Okay, so it looks like weve, successfully kind of generated some random numbers, so thats pretty cool. Well now what we want to do is light up some LEDs based on these random numbers. So how are we going to do that? So all I should have to do to turn on some LEDs based on this random number is use this to do a digital write to one of the pins, so lets go ahead and do that. So what this line of code will do is write high voltage to the pin number specified by random number and right now the pin number being specified by random number is a pin between 0 amp 4, because remember that 5 is not inclusive 0 amp 4.
But for our purposes I dont have any LEDs attached to pins 0 or 1. So what do we have to do here? We have to modify this random. So if you recall we can insert a minimum number so Im going to say the minimum number will be 2. So now random is going to return a number that is between 2 amp 4, because remember that final number is not inclusive, so random number will be assigned either 2, 3 or 4. Every time we go through the loop, then every time we go through the loop were going to write that different random number. Now. What I want to do also is delay some time and then turn the LED off so that we can see the effect so lets do that so Ive added a delay of 100 milliseconds and then I do a digital write, random number low, so thats going to Turn the LED off now notice. This random number is the same random number here, so we dont assign a new random number before we get to this line of code, so we can ensure that that variable will stay the same from when we turn it on to turning it off all right. So lets go ahead and take a look at the board and see what this looks like. We can see that our LEDs are doing fairly random things, which is neat, and you can tell I mean this – is pretty easy to do this isnt anything crazy.
There are a couple intricacies that we talked about with the random function and the random seed that you should know about now. So hopefully this has been a helpful tutorial, hey. I cannot wait to see you next time.