انتقل إلى وضع عدم الاتصال باستخدام تطبيق Player FM !
المدونة الصوتية تستحق الاستماع
برعاية


1 Venture Investing in Mobility + Tech with University of Michigan’s Early-Stage Zell Lurie Commercialization Fund 39:30
Arduino Pseudo Random Non-Consecutive Number Generator
Manage episode 269164106 series 2774299
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 functionIn 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, that way we keep updating our previous random number every time through the loop().
Let’s Implement our solution in an Arduino Sketch.Copy and paste this code into your Arduino IDE. All you need is an Arduino board attached to your computer to make it work.
//This sketch outputs pseudo random non-consecutive integers. //A variable to hold pseudo random non-consecutive integers. int randomInt = 0; //A variable to hold the previously assigned pseudo random non-consecutive integers. int previousRandomInt = 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); /*Check if the random number is the same as the previous random number. If it is, then reassign a new random number until it is different from the previously set one.*/ while (randomInt == previousRandomInt) { //Create a random number and assign it to the randomInt variable. randomInt = random(0, 10); //When a consecutive random number has been identified, indicate it. Serial.println(); }//close while statement //Set previousRandomInt equal to the current randomInt. previousRandomInt = randomInt; //Send randomInt to the serial port for displaying on the serial monitor window. Serial.print(randomInt); }//Close loop function.If you upload this code to your Arduino and open the serial monitor window you will see the numbers scrolling across the serial monitor window, but now you will not witness any duplicates side-by-side. You may notice some X’s intermittently between the numbers, this is where the Arduino sketch identified a duplicate random number and generated a replacement.
If you look through the code, you will see this is accomplished with a While Statement.
I hope you can find some application for this simple process of creating a pseudo random non-consecutive number with Arduino. Let me know what you think the comments!
61 حلقات
Manage episode 269164106 series 2774299
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 functionIn 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, that way we keep updating our previous random number every time through the loop().
Let’s Implement our solution in an Arduino Sketch.Copy and paste this code into your Arduino IDE. All you need is an Arduino board attached to your computer to make it work.
//This sketch outputs pseudo random non-consecutive integers. //A variable to hold pseudo random non-consecutive integers. int randomInt = 0; //A variable to hold the previously assigned pseudo random non-consecutive integers. int previousRandomInt = 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); /*Check if the random number is the same as the previous random number. If it is, then reassign a new random number until it is different from the previously set one.*/ while (randomInt == previousRandomInt) { //Create a random number and assign it to the randomInt variable. randomInt = random(0, 10); //When a consecutive random number has been identified, indicate it. Serial.println(); }//close while statement //Set previousRandomInt equal to the current randomInt. previousRandomInt = randomInt; //Send randomInt to the serial port for displaying on the serial monitor window. Serial.print(randomInt); }//Close loop function.If you upload this code to your Arduino and open the serial monitor window you will see the numbers scrolling across the serial monitor window, but now you will not witness any duplicates side-by-side. You may notice some X’s intermittently between the numbers, this is where the Arduino sketch identified a duplicate random number and generated a replacement.
If you look through the code, you will see this is accomplished with a While Statement.
I hope you can find some application for this simple process of creating a pseudo random non-consecutive number with Arduino. Let me know what you think the comments!
61 حلقات
كل الحلقات
×
1 Arduino Course for Absolute Beginners 2nd Edition 3:20

1 How to change the font color in the Arduino IDE 4:55

1 Use Serial.print() to display Arduino output on your computer monitor: Part 2 6:25

1 Use Serial.print() to Display Arduino output on your computer monitor: Part 1 8:36

1 How to make a secret knock detector to trigger anything with only an Arduino and a few cheap components 12:57

1 Arduino Pseudo Random Non-Consecutive Number Generator 11:13

1 Understanding the Arduino Sketchbook: Opening and Saving Arduino Sketches 9:41

1 An Easy Way to Learn I2C, SPI, RTC, ADCs and More with this Awesome Arduino Education Shield 5:04

1 The Process and Tools I use for Creating Arduino Tutorials 8:10

1 Using the Same Input to Trigger Multiple Arduinos 9:41

1 6 Tips on Assembling an Arduino Shield (Or any Electronics Kit) 8:15

1 A YouTube Channel for Learning about Arduino 5:34

1 How to Use and Understand the Arduino Reference 12:48

1 Using Red-Green-Blue (RGB) LEDs with Arduino (Common Cathode Type) 14:47

1 How to Make One Button Have the Functionality of Two or More with Arduino 15:40

1 Understanding HIGH and LOW Arduino Pin States 12:31

1 Floating Pins, Pull-Up Resistors and Arduino 10:29

1 The MOST guaranteed way to NOT buy a Fake Arduino (The Story of Pizza-Duino) 5:48

1 Throw out your breadboard! Dr. Duino: An Arduino Shield for debugging and developing Arduino projects 11:35

1 Shorthand Arithmetic :: Using Compound Operators (+= , -= , *= , /= ) with Arduino 12:43

1 Understanding Boolean Data Types and Using the Boolean NOT (!) operator to Switch Arduino Pin States 8:11

1 What to do when you just don't know :: Arduino, ADXL345 triple axis accelerometer and an RGB LED 14:04

1 3 Ways to Use Acceleration in an Arduino Sketch 17:37

1 Check out our premium Arduino Training Course 2:16

1 Understanding the Arduino uber functions loop() and setup() 10:26

1 Functions: Let's make programming Arduino as easy as possible 11:00

1 Syntax; the spelling and grammar of programming 11:32

1 The Arduino Development Toolchain - How it all gets done... 8:40

1 Arduino Board Hardware overview for people like me 10:38

1 Everything you need to know about the Arduino IDE (for now) 11:09

1 Mac - Download and Install the Arduino IDE 7:16

1 PC - Download and Install the Arduino IDE 7:10

1 Arduino Integrated Development Environment Version 2:24




1 How to read voltages with analogRead() 14:44










1 What can you do with Arduino? Let's get pumped! 3:12


1 Arduino Lessons from Programming Electronics Academy 2:22
مرحبًا بك في مشغل أف ام!
يقوم برنامج مشغل أف أم بمسح الويب للحصول على بودكاست عالية الجودة لتستمتع بها الآن. إنه أفضل تطبيق بودكاست ويعمل على أجهزة اندرويد والأيفون والويب. قم بالتسجيل لمزامنة الاشتراكات عبر الأجهزة.