Missing Logic Looping Through Switch Statement

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':

$ ./test
Gimme something: cheese

a4d19a86
1ad33008
85f31aae
85f31aae
3927bca6
85f31aae

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.

What is this?

ranit(tilday_array)

Maybe it should be:

ranit(tilda_array)

?

Wow... That was actually a bad copy paste while making a snipet to fit in this post. This is what I should have had:

for (j = 0; data[j] != 0; j++){
   switch(data[j]){
      case 'c':              output[j] = ranit(c_array);
             break;     
     case 'h':              output[j] = ranit(h_array);             
            break;                 
     case 'e':              output[j] = ranit(e_array);             
            break;
     case 's':              output[j] = ranit(s_array);             
            break;     
     default:                 
            break;                  
     }      
}       

for (i = 0; output != 0; i++) {          
     printf("%x\n", output);      
}

Hopefully that makes more sense now.

Why and how do you expect varying results i.e. non-repeating numbers when running functions with non-varying, constant parameters?

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

awk 'BEGIN {srand(NULL); for (i=1; i<=10; i++) print rand()}'
0.131538
0.755605
0.45865
0.532767
0.218959
0.0470446
0.678865
0.679296
0.934693
0.383502
awk 'BEGIN { for (i=1; i<=10; i++) {srand(NULL); print rand()}}'
0.131538
0.131538
0.131538
0.131538
0.131538
0.131538
0.131538
0.131538
0.131538
0.131538

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.

Just remove the srand() from your function.

2 Likes

That did indeed work!

$ ./test
Gimme something: cheese

b2f14f45
f21f4f7a
1c1bc775
20737099
af749247
327683d

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!