Go Down

Topic: Random() (Read 1 time) previous topic - next topic

Coding Badly

There seems to be a few other discussions happening here... 


Which now ends.  Any mention of the off-topic subject "true random" will be removed from this thread.  Either help @iluvplanes or move along.



Which now ends.  Any mention of the off-topic subject "true random" will be removed from this thread.  Either help @iluvplanes or move along.



Thanks for your help, @Coding Badly. It's greatly appreciated.


Let me give it a try:
code...
A little bit tighter.



@dhenry, Thanks for your help!




From your code, it appears that you can flash an LED multiple times in the cycle--just not twice in a row, and that within a cycle it is ok if an LED doesn't flash at all. If not, then Nick's shuffle approach is the way to go.




Thanks for pointing that one out, Pat.
I fixed it, so here's the edited part.

Code: [Select]

//The third LED
while (randNumber1 == val || randNumber1 == val2)  {
    randNumber1 = random(2,6);
  }

//The fourth LED
while (randNumber1 == val || randNumber1 == val2 || randNumber1 == val3) {
    randNumber1 = random(2,6);
  }


patduino

Almost there!

Be sure to "remember" the previous values - see example below for val3.

Code: [Select]
//The third LED
while (randNumber1 == val || randNumber1 == val2)  {
    randNumber1 = random(2,6);
    val3 = randNumber1;
    // light LED here...
  }

//The fourth LED
while (randNumber1 == val || randNumber1 == val2 || randNumber1 == val3) {
    randNumber1 = random(2,6);
  }

There are 10 types of people in the world, those that understand binary, and those that don't.


Almost there!

Be sure to "remember" the previous values - see example below for val3.


Actually, I did that... :) I forgot to add it in my copied code. It's in the section where I light the LED
Here's the full code for LEDs 3 & 4.
Code: [Select]

randNumber1 = random(2,6); 
  while (randNumber1 == val || randNumber1 == val2)  {
    randNumber1 = random(2,6);
  }
  //Write to 3rd LED
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);
    val3 = randNumber1;

 
  //Check
  randNumber1 = random(2,6);
  while (randNumber1 == val || randNumber1 == val2 || randNumber1 == val3) {
    randNumber1 = random(2,6);
  }

  //Write to 4th LED.
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);
    val = randNumber1;

dhenry

You don't really need to do any of that.

All you wanted is to randomly pick one led to light up.

So something like this will do:

Code: [Select]

  randNumber1 = random(2,6); 
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);


Run it in a loop and you will see one led lighting up.

A much better implementation is actually what I wrote earlier: you generate a random pattern and use that pattern to determine which leds will be on / off.

patduino

Excellent!  On to he next project?
There are 10 types of people in the world, those that understand binary, and those that don't.


Excellent!  On to he next project?


On to the next project it is! Or, actually, a further, more complicated continuation of this project. I'll have the details to show you soon! :)

Go Up