random() help

Hey i was wondering if someone could help me with a problem

i have four push buttons
i have four LEDS

i want to use random(1,4)

then delay(1000); /// in between lighting up each LEDS

i want to light up 4 LEDS randomly and never repeat but if one of the push buttons gets pressed at the same time that same LED is lit (button 1 and LED 1 ) it stays lite then regenerates my random min and max (1,3) but here is my problem

how might the code look if LED3(button 3 pressed) and i want to regenerate the random min and max?

i need to leave out 3....random(1,2,4)

devanlynk:
how might the code look if....

How might the code look right now? It is important to provide as much of the information that is needed to solve your problem as you can, in your first posts. The forum has guidelines for posting in a standard way that makes it easiest for people to provide you with useful answers. Making an effort to do this will greatly increase the number and quality of helpful responses that you get.

Please post your code, using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.

I'm not going to try and write the code for you because I don't have the time to write, test, and debug it, but...

If I understand your goal, this is called "sampling without replacement". (Like pulling names from a hat and not putting it back.)

Do you know how to make an array?

  • Make an array that holds all of the variables.

  • Randomly select one element from the array.

  • Move all of the elements above the selected one down (or left) by one, overwriting the one that was selected/used.

  • Reduce the random range by one so you don't select the duplicated values. (The random-maximum has to be a variable).

  • Of course, you'll have to keep count and stop after you've selected all four LEDs.


...If you just don't want the same LED twice in a row, and you want to loop forever, then you simply save the least value as a variable and try-again if you get a matching random number.

i don't have code.... i wanted to know if is there anther way to have in between numbers left out...that's it in a nutshell

random(1,2,4) leave out 3

If you have no code, what is your “problem”? A problem is different than curiousity. A real program in front of you, provokes thoughts in a way that imagining it can’t do.

devanlynk:
i don't have code.... i wanted to know if is there anther way to have in between numbers left out...that's it in a nutshell

random(1,2,4) leave out 3

Code tells the compiler what you want the machine to do, exactly.
If you don't know the language then how will you tell the compiler jack?
If you do know the language even through beginner level then you'd already be writing about an array and thinking of different ways to have it work as desired. An expert would use 4 bits of a byte as the array but that's not required, a beginner who took the time to learn Arduino/C can do this.

Do you know what an array is?

devanlynk:
random(1,2,4) leave out 3

byte selection;
do {
  selection = random(1, 4);
} while (selection == 3);

So: take a random number 1-4 (why not 0-3? Makes picking the LED much easier as your array - you do use arrays, don't you? - also counts 0-3) until you have one that's not 3.

wvmarle:

byte selection;

do {
 selection = random(1, 4);
} while (selection == 3);



So: take a random number 1-4 (why not 0-3? Makes picking the LED much easier as your array - you do use arrays, don't you? - also counts 0-3) until you have one that's not 3.

The second argument to random() is outside the range so that should be random(1,5) to get values between 1 and 4.

Here’s some code to get the OP started. It does the first part (picking LEDs to light in random order). After this it’s time to add buttons but I can’t figure out from the description what the buttons are supposed to do.

const byte LEDCount = 4;
const byte LEDPins[LEDCount] = {4, 5, 6, 7};

boolean LEDLit[LEDCount];

void setup()
{
  Serial.begin(115200); // For debug output

  for (int i = 0; i < LEDCount ; i++)
  {
    pinMode(LEDPins[i], OUTPUT);
    digitalWrite(LEDPins[i], LOW); // OFF
    LEDLit[i] = false;
  }
}


void loop()
{
  static byte ledsLit = 0;

  // Light each one in random order
  if (ledsLit < LEDCount)
  {
    byte index;
    do
    {
      index = random(LEDCount);
    } while (LEDLit[index]);  // Repeat if already lit
    digitalWrite(LEDPins[index], HIGH);
    LEDLit[index] = true;
    ledsLit++;
    Serial.println(index);  // debug
    delay(1000);
  }
  else
  {
    // This happens when all of the LEDs are lit.
  }
}

devanlynk:
i don't have code.... i wanted to know if is there anther way to have in between numbers left out...that's it in a nutshell

random(1,2,4) leave out 3

This is why I ask if you know about arrays, so I know where to start an answer.

Learn about arrays, really learn not just skim a page and think that's it, and Reply #2 should make sense.

In your IDE are quick lessons that you can mess with and learn from. You have to DO to really learn, for neural connections in your head to grow what short term memory loses soon. It's the opposite of cram and forget later.

Open the IDE and a web browser tab,
Arduino IDE --> File menu: Examples --> 05.Control --> Arrays
browser: https://www.arduino.cc/en/Tutorial/Arrays

When you get to the 05.Control menu with Arrays you also see the other sketches. Arrays show you ways to organize and store data (like pin numbers) and the control structures show how to work on it. Do the examples, load them, run them, read the code and the help and you should see how they work. Once you do that you can think in those terms and your first benefit will be being able to ask more direct, specific questions that get you shorter specific answers.