Pages: 1 [2]   Go Down
Author Topic: Random()  (Read 1418 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12777
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


@dhenry, Thanks for your help!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



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:
//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);
  }

Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Almost there!

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

Code:
//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);
  }
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Almost there!

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

Actually, I did that... smiley 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:
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;
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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.
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Excellent!  On to he next project?
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley
Logged

Pages: 1 [2]   Go Up
Jump to: