void display::randomizer(void){//picks random images 1-18 to display
int i=0;
int temp=0;
int rando=0;
#ifdef Q_WS_WIN
LARGE_INTEGER cycles;
QueryPerformanceCounter(&cycles);
srand(cycles.QuadPart);
#endif
for(i=1;i<=18;i++){
rando = rand() % 18 + i;
if(rando>18){
rando=18;
}
temp = r[i];
r[i]=r[rando];
r[rando]=temp;
}
}
The array 'r' is used to pick 18 random letters and display them. Sometimes, very rarely, there are bad words that CAN appear. I have used the program a whole lot and never seen them, but I do understand that there is a statistical possibility.
I tried lfind() in <search.h>, but I can only get it to tell me if there is a certain single int inside the array. What I would like is a quick way to search for these arrays, as I don't want any noticeable slowdown and the user is able to ask for random values many times per second.
I apologize if I have overlooked anything, I am probably searching using the wrong terms.
//Initialize Badword array
staticint badwords[7][4]=BADWORDS;
//Create compare function for lfind() to use
extern"C" {
int compare(constvoid *a,constvoid *b){
return(*(int *)a-*(int *)b);
}
}
//Create a checkForBadWords function
void display::checkForBadWords(void){
void *pFirst=NULL;
size_t size=19;
int j=0;
for(i=0;i<NUMBER_OF_BADWORDS;i++){
pFirst=lfind(badwords[i],&r[1],&size,sizeof(int),compare); //lfind should always return a pointer here
if(pFirst){ //check to make sure it did its job
if(*((int *)pFirst+3) != 0){ //check the fourth int to make sure it is something real
if((badwords[i][1] == *((int *)pFirst+1)) && (badwords[i][2] == *((int *)pFirst+2)) && (badwords[i][3] == *((int *)pFirst+3))){//check to see if the rest of the word is there
for(j=1;j<=18;j++){//If I find a bad word, standardize the array
r[j]=j;
}
return;//don't check for other bad words, because one is enough
}
}
}//move on to the next word
}
}
I extended the array 'r' to contain zeros at the end, that way if lfind() returns a pointer to the last real value in 'r', there won't be any overflow when checking against the remaining values.
Like I said, this works great for seven 4-letter words...but I am sure there are n, N-letter words that need to be removed in some math class somewhere, and this solution will not work for that.