1. ## while condition help

Here is a snippet of code that I'm trying to generate random numbers. I only want to return when the random number is divisible evenly by 2 or 3:
Code:
```int genRandNum (){int z = 1, min = 0, max = 1000;                //sets local variables for random number generationprintf("Generating random number\n"); //test line        srand ( time (NULL) );                    //seeds random number generator

do{                                        //loop to generate number

z =  (min + ((rand() * (1.0 / (RAND_MAX))) * (max - min)));        //found calc on internet
printf("generated: %d\n",z);  //testing line

}while ((z%2) == 0 || (z%3) == 0);                //making sure random number fits criteria
printf ("returned value = %d\n",z); //testing line
return z;                                //returns valid value
}```
Here is the output from the latest run:

Generating random number
generated: 56
generated: 108
generated: 919
returned value = 919

I would appreciate any constructive feedback on why it didn't stop and return the 58 OR the 108.

2. Code:
`       }while ((z%2) == 0 || (z%3) == 0);`
This condition is wrong. You want to negate them:
Code:
`      } while (z%2 != 0 || z%3 != 0);`

3. Originally Posted by Kent Luttrell
I only want to return when the random number is divisible evenly by 2 or 3:
There is no need to change your condition, however you need to retranslate your English to C-compatible English:
"Keep generating random numbers, but stop as soon as the generated number is divisible by 2 or 3:"

Code:
```for(;;){  // keep generating random numbers until I say stop
z =  (min + ((rand() * (1.0 / (RAND_MAX))) * (max - min)));
printf("generated: %d\n",z);
if ((z%2) == 0 || (z%3) == 0)  //z is divisible by 2 or 3, so stop
break;
}```

4. @nonpuz:: That fix did what I wanted it to do, thank you so much. Although, now it is always generating the same number or set of numbers. I have used the time-random seed to try to avoid this. Any more input would be greatly appreciated.

@c99tutorial:: I don't want to recode my "previously working" method. This is an assignment and I don't care to lose points doing code that I obviously haven't encountered. Now if you have any input on the seeding issue, that would be appreciated.

5. You are seeding with srand(time(NULL)) which will work "okay". For a "better" seeding you might want to include <sys/time.h> and then do something like this:

Code:
```struct timeval t1;
gettimeofday(&t1, NULL);
srand(t1.tv_usec * t1.tv_sec);```
Seeding srand() by Guy Rutenberg

6. With another alteration you can fix the duplicate issue here by not calling srand() every time you generate 1 random number. You should really
only seed once per run unless you are a daemon that is up for days on end or something like that. Upon reloading config for example could be another good time.

If instead you use a static variable to determine whether or not rand has been seeded then you only do it the first time you call genRandNum and
get much better output:
Code:
```int genRandNum(void)
{
static enum { OFF, ON } init;
int z = 1, min = 0, max = 1000;                //sets local variables for random number generation
printf("Generating random number\n"); //test line

if (init != ON) {
srand ( time (NULL) );                    //seeds random number generator
init = ON;
}
...
...```
Output:
Code:
```Generating random number
generated: 641
generated: 901
generated: 167
generated: 75
generated: 128
generated: 830
generated: 734
generated: 178
generated: 965
generated: 889
generated: 12
returned value = 12
Generating random number
generated: 383
generated: 861
generated: 723
generated: 821
generated: 77
generated: 67
generated: 829
generated: 917
generated: 798
returned value = 798
Generating random number
generated: 890
generated: 881
generated: 26
generated: 208
generated: 752
generated: 359
generated: 582
returned value = 582
Generating random number
generated: 760
generated: 582
returned value = 582```

7. Every time I do something other than srand (time(NULL)), it messes with the while condition. @nonpuz, even in your code it bypassed numbers that met the criteria. This is what is happening every time.

Sorry if I'm taxing your patience, I do appreciate all the input. I have not actually been taught C. We were given some basics and told to pick up what ever we could on our own. This is all part of the classic "unisex bathroom" problem. I am using the modulus to determine if they are male or female.

8. Thats because the first condition I gave you was still wrong (Although I originally posted it correctly then changed it to the wrong thing )

Here is the correct condition for your loop to achieve what you need:
Code:
`    }while ((z%2) != 0 && (z%3) != 0);`