 # How to randomly pick a number of pins from an array, what am I looking for?

Hi.
I’ve been googling for hours trying to find a way to randomly pick a number of pins from an array.
Hopefully my sketch make sense, it’s not pretty but doing my best here.
Tried to explain it as good as I can and my problem is //-out in the middle.

``````int pinArray[] = {2, 3, 4, 5, 6, 7};
int count = 0;
int Antal;
int Tid;
int Tid2;

void setup(){
// we make all the declarations at once
for (count=0;count<6;count++) {
pinMode(pinArray[count], OUTPUT);
}
}

void loop() {
//randomize between 0 and 5
Antal = random(5);

//Allocate 0 and 1 as 6 and 7
if (Antal==0) {Antal=6;}
if (Antal==1) {Antal=7;}

//Randomize time
Tid = random(2000, 6000);

//Randomize time2
Tid2 = random(2002, 10000);
//
//
//
//Randomly pick "Antal" number of pins from array

//Set picked pins HIGH

//Run for "Tid"
delay(Tid);

//Picked pins LOW
//
//
//

//Clear "Tid"
if (Tid=>0){Tid==0;}

//Pause for "Tid2"
delay(Tid2);

//Clear "Tid2"
if (Tid2=>0){Tid2==0;}

//restart by choosing new "Antal"

}
``````

What do you want to do?
What is the objective here?
Pushing a random twelve-bit (or *n-*bit) binary number onto the output pins (2-13) - or what?

https://www.arduino.cc/reference/en/language/functions/random-numbers/random/

The upper bound is excluded so "//randomize between 0 and 5" should say "//randomize between 0 and 4". If you don't want the range to start as 0, use the two argument version:

``````Antal = random(2, 8);  // Random pin from 2 to 7
``````

What do you mean by "randomly pick a number of pins from an array"? Do you want a random number of randomly selected pins? Do you want the pins not to repeat (so the same pin won't be used twice in a selection)? If everything is random, are you sure it matters?

Note: This does not "Clear Tid". You are using "==" so it compares Tid to 0 and throws away the answer.

``````//Clear "Tid"
if (Tid=>0){Tid==0;}
``````

Same for Tid2. If you want to assign a new value, use "=", not "==".

The idea with the sketch is to select a random number within a set range, select that number of pins randomly from an array and set those pins high for a random time.
Random here is random within a set span.

Regarding the delays I figured I can use delay(random(2000, 6000)); instead and get a cleaner sketch.
Some other minor tweeks to the code will be made too but haven't gotten to it yet.
Still struggeling with the over all function You could define a new array the same size as the pin array and use the values in it as indices into the pin array.

The steps would be:

• Get the number of pins you want to use
• Generate that number of random indices, not repeating values
• Use the array of indices to index into the pin array
``````const byte arraySize = 6;
int pinArray[] = {2, 3, 4, 5, 6, 7};
int pinIdx[] = { -1, -1, -1, -1, -1, -1};
int count = 0;
int Antal;

void setup() {

Serial.begin(9600);

}

void loop() {

// get # of pins
Antal = random(1, arraySize+1);
fillIndexArray();

Serial.print("Count = "); Serial.println(Antal);

Serial.print("Values:  ");

for (int i = 0; i < Antal; i++)
{
// print index #
Serial.print("Index:  ");
Serial.print(pinIdx[i]);
Serial.print("  ");

// print pin #
Serial.print("Pin:  ");
Serial.print(pinArray[pinIdx[i]]);
Serial.print("  ");

}

Serial.println();

delay(1000);

}

void fillIndexArray()
{
int idx = 0;
int newIdx;
byte i;

// clear index array
for (i = 0; i < arraySize; i++) pinIdx[i] = -1;

// set first value in array of indices
pinIdx[idx++] = random(arraySize);

// while we need more indices
while ( idx < Antal )
{
// get next candidate
newIdx = random(arraySize);

// look for it on our list
for (i = 0; i < idx; i++ )
{

if ( pinIdx[i] == newIdx ) break;

} // for

if ( i == idx ) pinIdx[idx++] = newIdx;

} // while

}
``````

johnwasser:

``````if (Tid=>0){Tid==0;}
``````

Same for Tid2. If you want to assign a new value, use “=”, not “==”.

Moreover `if (Tid=>0) `… do you mean `(Tid>=0) `?? and if you are going to assign 0 it may as well be `(Tid>0)`

Thank you all for good advice.
I selected to redo the whole sketch w/o arrays and now it work just fine.
Rly have to get my head around arrays but just didn’t have the energy today.

Here’s the alternate sketch, sry for swedish comments.

``````int pin1 = 2;
int pin2 = 3;
int pin3 = 4;
int pin4 = 5;
int pin5 = 6;
int Antal;
int Vilka;

void setup() {
// put your setup code here, to run once:
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);

//Start Random

}

void loop() {
// put your main code here, to run repeatedly:

//Slumpa Antal 1-5
Antal = random(5)+1;

//Slumpa vilka pins 1-5
for (Vilka = 0; Vilka < Antal; Vilka++){
if (Pads == 1) digitalWrite(pin1, HIGH);
else if (Pads == 2) digitalWrite(pin2, HIGH);
else if (Pads == 3) digitalWrite(pin3, HIGH);
else if (Pads == 4) digitalWrite(pin4, HIGH);
else if (Pads == 5) digitalWrite(pin5, HIGH);
}

//Slå på tid
delay(random(1000, 6000));

//Slå av
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
//Vänta tid

delay(random(2000, 10000));
}
``````

Something like this?

``````const byte pinArray[] = {2, 3, 4, 5, 6, 7};
int Antal;

void setup()
{
for (int i = 0; i < 6; i++)
{
digitalWrite(pinArray[i], LOW);
pinMode(pinArray[i], OUTPUT);
}

}

void loop()
{
// Pick a number of pins between 1 and 6
Antal = random(1, 7);

// Turn on 'Antal' pins.  Pins can repeat so not always 'Antal' will light up.
for (int i = 0; i < Antal; i++)
{
byte pin;
pin = pinArray[random(0, 6)];
// Alternately:  pin = random(2,8);
digitalWrite(pin, HIGH);
}

delay(random(2000, 6000));  // Wait for 2 to 5.999 seconds

for (int i = 0; i < 6; i++)
{
digitalWrite(pinArray[i], LOW);
}

delay(random(2002, 10000));  // Wait for 2.002 to 9.999 seconds
}
``````