How can I ensure these three numbers are not equal?

closed account (E8A4Nwbp)
At the beginning of a function I declare three random values like so:

1
2
3
  int commencer = rand() % somevector.size();
    int midst = rand() % somevector.size();
    int concluder = rand() % somevector.size();


How could I ensure all three are different?

I use these three values in many places in the function, and they MUST be different to each other.

I use the three values in numerous conditionals, so I would want to avoid having to re-specify an if statement in each switch case
Last edited on
1
2
3
4
if (commencer != midst && commencer != concluder)
{
  //values are different
}
closed account (E8A4Nwbp)
Thanks, @TheToaster But then if they're the same, the things in the curly brackets won't be called
That is correct. The if statement will only be executed if all three values are different, as you have stated in your comment. If you want the if statement to execute only if all three are the same, then replace '!=' to '=='.
Last edited on
@TheToaster

And what if midst is the same as concluder?

Shouldn't it then be..
if (commencer != midst && commencer != concluder && midst != concluder)
closed account (E8A4Nwbp)
@TheToaster

That is correct. The if statement will only be executed if all three values are different, as you have stated in your comment. If you want the if statement to execute only if all three are the same, then replace '!=' to '=='.


But I need the three to ALWAYS be different, I can't take a chance, as the task to be executed must be called, and the three values have to be different
Topic archived. No new replies allowed.