I have an array with 8 spots to fill and 4 ones and 4 zeros to fill it with, but want the locations to be random. I am a bit stumped on how to do this without it being to redundant. Is there a simple way to do this or will I have to fill the array with either a 1 or a zero randomly then go through and check to see if it has the right number of both after the fact? My current code is along the lines of
void setup() {
int one =0;
randomSeed(analogRead(A0));
for (int i=0;i<8;i++){
array[i] = random(0,2);
if (array[i] == 1){
one++;
}
if(one >4){
array[i]=0;
one--;
}
}
}
This mostly works but has the some obvious drawbacks so any pointers on a better way to do this would be appreciated. thank you!
Aucun commentaire:
Enregistrer un commentaire