Skip to content

Instantly share code, notes, and snippets.

@gallaugher
Last active June 5, 2018 21:12
Show Gist options
  • Save gallaugher/5d19fbc190f35814c3e3ea30cbccb5e9 to your computer and use it in GitHub Desktop.
Save gallaugher/5d19fbc190f35814c3e3ea30cbccb5e9 to your computer and use it in GitHub Desktop.
Txplore Arduino LBD Day 4, Example 1 - ten random numbers, even or odd
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// put this here so that each time I restart with button press I have a line feed gap so I can easily compare with prior results
Serial.println();
randomSeed(analogRead(0)); // supposed to make sure random simulates (pseudo random) a random number.
// uncomment for mini project 1
ten_random_numbers();
// mini project 2
// while_ten_random_numbers();
}
void loop() {
// put your main code here, to run repeatedly:
}
void ten_random_numbers() {
for (int i = 0; i < 10; i++ ) { // go through this 10 times, i will equal 0 to 9
int number = random(100); // generate a random # from 0 to 99
Serial.print(number); // print the number.
if ((number % 2) == 0) { // if number / 2 has no remainders...
Serial.println(" EVEN"); // it's even
} else {
Serial.println(" ODD"); // otherwise it's odd.
}
}
}
void while_ten_random_numbers() {
byte index = 0;
while (index < 10) {
int number = random(100); // generate a random # from 0 to 99
Serial.print(number); // print the number.
if ((number % 2) == 0) { // if number / 2 has no remainders...
Serial.println(" EVEN"); // it's even
} else {
Serial.println(" ODD"); // otherwise it's odd.
}
index++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment