Discover this podcast and so much more

Podcasts are free to enjoy without a subscription. We also offer ebooks, audiobooks, and so much more from just $11.99/month.

Arduino Pseudo Random Non-Consecutive Number Generator

UNLIMITED

Arduino Pseudo Random Non-Consecutive Number Generator

FromLearn Programming and Electronics with Arduino


UNLIMITED

Arduino Pseudo Random Non-Consecutive Number Generator

FromLearn Programming and Electronics with Arduino

ratings:
Length:
11 minutes
Released:
Apr 23, 2017
Format:
Podcast episode

Description

In this video we demonstrate how to create pseudo random numbers with Arduino - with a useful twist. This lesson was inspired by the following viewer question: How do I create Random Non-Consecutive numbers with Arduino? P.S. These are the best tutorials that a complete idiot like you could ever make, thanks. -Chris Let's overview exactly what we will talk about in today's episode: Talk about pseudo random numbers. Identify the problem - using an Arduino sketch to demonstrate. Discuss how we might solve the problem. Write an Arduino sketch that solves the problem. Review what we talked about. Before we answer the viewer’s question it is important to talk about what a pseudo random number is. A purely random number in the mathematical sense can't be predicted. The microcontroller that the Arduino uses (and for that case, most computers in general) can't really create pure random numbers. What they create instead are called pseudo random numbers. These are numbers that appear to be randomly generated, but if studied over time a predictable pattern emerges. The bottom line is that the random numbers we create with Arduino can be predicted. Now there are clever ways to create pseudo random numbers that act like the real deal – you can learn about one method in our video tutorial talking all about random numbers – but for this discussion, let’s return to our viewers inquiry. Identify the Viewer’s Problem - use an Arduino sketch to demonstrate. Ok, so let's go back to the viewers question, he wants to generate random numbers, but he never wants the same number generated two times in a row. Let's write an Arduino Sketch to make this clear. //This sketch outputs pseudo random integers. //A variable to hold pseudo random integers. int randomInt = 0; void setup() { //Initiate serial communication. Serial.begin(9600); }//Close setup function void loop() { //Create a random number and assign it to the randomInt variable. randomInt = random(0, 10); //Send randomInt to the serial port for displaying on the serial monitor window. Serial.print(randomInt); }//Close loop function. In the first block of code a variable that will hold the pseudo random integers is declared and initialized. //A variable to hold pseudo random integers. int randomInt = 0; In the setup() function we begin serial communication in order to display the numbers we generate on a computer display. void setup() { //Initiate serial communication. Serial.begin(9600); }//Close setup function In the loop() we create the random number with the Arduino random() function and assign the output to the variable we had just created. The random() function can take two arguments 1) the minimum value of the number we want generated 2) the maximum value we want generated. //Create a random number and assign it to the randomInt variable. randomInt = random(0, 10); I will use 0 for the minimum, and 10 for the maximum. Every time through the loop, a new random number will be assigned the randomInt variable. Finally, the value of randomInt is sent over the serial port to be displayed in the serial monitor window. //Send randomInt to the serial port for displaying on the serial monitor window. Serial.print(randomInt); If you upload this code and open the serial monitor you will see in some cases where the same number shows up two times in a row. This is the problem.  The viewer doesn't ever want the same number two times in a row.  Discuss how we might solve the problem. So let's talk about how we might solve this problem.  We know we need to generate a random number. What if we create a variable to track the previous random number? Then we could use a condition that says something like "If the previous random number is equal to the random number that was just generated, toss that number out the window, and create a different one.” The final thing we would need to do is set the previous random number equal to the new random number,
Released:
Apr 23, 2017
Format:
Podcast episode

Titles in the series (61)

Video lessons on learning programming and electronics with Arduino. This is part of our Arduino Crash Course and Arduino Course for Absolute Beginners. It's designed to take someone with little or no experience in programming and electronics and get them fast-tracked to learning the skills to prototype using Arduino. We'll include some lessons from the first edition and the second edition of our training course.