Having a Problem With Random Variables

I have three int variables, A B and C. Both A and B are a randomly selected number between 1 and 3. It is possible for A to equal B. I believe I have them set up correctly.

A = rand() % 3 + 1;
B = rand() % 3 + 1;

C also needs to be a randomly selected number between 1 and 3, but must never equal A or B. How would I go about assigning it?

1
2
3
4
5
C = rand() % 3 + 1;
while (C == A || C == B)
{
   C = rand() % 3 + 1;
}
code duplication is bad. use a do/while instead:

1
2
3
4
do
{
    C = rand() % 3 + 1;
}while(C == A || C == B);
Topic archived. No new replies allowed.