Go Down

Topic: how to generate unique random number between 0 and 10 (Read 151 times) previous topic - next topic

marwan12

i have array filled with 10 strings d[]={"strint1",string2",................................}
and i have empty array
i want to fill the empty array with strings from array d but randomly
and i dont want the same string to be filled

i tried this code
//#include <LiquidCrystal.h>
char* s[]={"string1","string2","string3","string4","string5"};
char* genWords[5];
int rounds;
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int bstatus=1;
uint8_t counter=0;
bool gameStatus=false;
int tones[]={300,600,900,1200,1500};

int bpin=1;
int buzzPin=2;

void setup() {
  pinMode(bpin,INPUT_PULLUP);
  // put your setup code here, to run once:

}

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

if(gameStatus==false){
  gameStatus=true;
  beginsq();
  rounds=0;
  ranOrder();
}
while(rounds<5){// lcd.setCursor(0, 0);
//lcd.clear();
//lcd.print(genWords[rounds]);

if(millis()-counter>30000){
  counter=millis();
  if(bstatus==1){
    losesq();
    gameStatus=false;
  }
}
if(digitalRead(bpin)==LOW){
  nextsq();
  rounds++;
  bstatus=0;
  while(digitalRead(bpin)==LOW){};
}
else { bstatus=1;}

 
}
if(rounds>5){
  winsq();
  gameStatus=false;
}

}





void nextsq(){
  for(int y=0;y<5;y++){
    tone(buzzPin,tones[y],150);
  }
 
}
void losesq(){
  for(int y=0;y<5;y++){
    tone(buzzPin,tones[y],150);
  }
 
}
void winsq(){
  for(int y=0;y<5;y++){
    tone(buzzPin,tones[y],150);
  }
 
}
void beginsq(){
  for(int y=0;y<5;y++){
    tone(buzzPin,tones[y],150);
  }
 
}











// to generate random order

void ranOrder(){
  int j;
  int i;
  int k;
  int c;
  int o;
  // s1 s2 s2 s3 s4
  for(i=0;i<4;i++){
                      j=round(random(0,5));
                      genWords=s[j];
                  } 
                  for(k=0;k<=4;k++){
                    for(o=0;o<=4;o++){if(k==o){continue;}
                      if(genWords[k]==genWords
){

                        do{
                        c=round(random(0,5));
                        genWords[k]=s[c];
                       
                      }while(genWords[k]==genWords
);

                     
                     
                    }
                   
                             
                                  }
 
}
}

AWOL

Now, hopefully, you can see why we ask you to use code tags when posting code.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

jremington

#2
Aug 10, 2018, 05:09 pm Last Edit: Aug 10, 2018, 05:46 pm by jremington
Once you have figured out how to post code properly, please post all of it.

Quote
i want to fill the empty array with strings from array d but randomly
Consider other approaches, such as a randomly arranged list of indices to the original strings (the operation is called shuffling).

UKHeliBob

Quote
i want to fill the empty array with strings from array d but randomly
and i dont want the same string to be filled
It would be much better to put index numbers to the first array in the second one rather than copy strings around and it is also much easier to check whether an index number has previously been used.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jremington

#4
Aug 10, 2018, 09:36 pm Last Edit: Aug 10, 2018, 09:37 pm by jremington
Use code tags when posting code.

Go back and edit your posts now, please.

Go Up