Go Down

Topic: Random() (Read 3495 times)previous topic - next topic

#15
Nov 20, 2012, 03:41 am
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.

iluvplanes

#16
Nov 20, 2012, 04:07 am

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

iluvplanes

#17
Nov 20, 2012, 04:09 am

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

iluvplanes

#18
Nov 21, 2012, 02:32 am

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 LEDwhile (randNumber1 == val || randNumber1 == val2)  {    randNumber1 = random(2,6);  } //The fourth LEDwhile (randNumber1 == val || randNumber1 == val2 || randNumber1 == val3) {    randNumber1 = random(2,6);  }`

patduino

#19
Nov 21, 2012, 03:03 am
Almost there!

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

Code: [Select]
`//The third LEDwhile (randNumber1 == val || randNumber1 == val2)  {    randNumber1 = random(2,6);    val3 = randNumber1;    // light LED here...  } //The fourth LEDwhile (randNumber1 == val || randNumber1 == val2 || randNumber1 == val3) {    randNumber1 = random(2,6);  }`
There are 10 types of people in the world, those who understand binary, and those that don't.

iluvplanes

#20
Nov 21, 2012, 03:13 am

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

#21
Nov 21, 2012, 03:24 am
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

#22
Nov 21, 2012, 03:34 am
Excellent!  On to he next project?
There are 10 types of people in the world, those who understand binary, and those that don't.

iluvplanes

#23
Nov 21, 2012, 07:16 am

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

Please enter a valid email to subscribe