Having trouble with the logic when looping over this switch case again:
for (j = 0; data[j] != 0; j++){
switch(data[j]){
case 'c':
output[j] = ranit(r_brace_array);
break;
case 'h':
output[j] = ranit(pipe_array);
break;
case 'e':
output[j] = ranit(tilday_array);
break;
case 's':
output[j] = ranit(comma_array);
break;
default:
break;
}
}
for (i = 0; output != 0; i++) {
printf("%x\n", output);
}
It calls a function (ranit) that assigns a letter a random hex number from an array and it works fine. However, if I give this program the phrase 'cheese' you will noticed that '85f31aae' is displayed for every instance of the letter 'e':
What I want is the function ranit() to be called again if a letter repeats. This way a repeating character does not get a repeating number. I tried making an additional outer loop, but the results did not change.
Again, had to post a code snippet due to length of code.
If anyone can help with me with the logic its greatly appreciated.
Maybe I should have posted more code. This is the function that is being called each time:
// Pull random array element to be called for each character.
int ranit(int char_array[]) {
srand ( time(NULL) );
int RandIndex = rand() % DATA_SIZE;
return char_array[RandIndex];
}
It selects a random in the array being called and works.
In another function all the arrays mentioned have a random number with /dev/urandom generated and assigned to each element.
Sorry if I was vague. I just have about 600 lines of code now and was trying to narrow down what was relevant to fit here. My apologies.
Calling random seed repeatedly is sort of overdoing it - or, in your case, even counterproductive. Do it once at program startup - if at all necessary as the compiler / interpreter may do it for you.
I know you are compiling a (C) program. Just as a hint, man awk explicitly says
Although you are calling srand() with a sort of random parameter, the current system epoch time, the execution will be that fast epoch seconds will not change - that's why you lose the random effect.
I knew that srand was based off the system clock and calculated fractions of seconds. I just didn't realize that a loop could iterate that fast I guess.
This also solves other problems I was having this program. Much appreciated!