i have a qt form that has a multiplication problem and 4 buttons. the 4 buttons (choices) are randomly generated and so is the question.
i just want to randomize the 4 choices so they are random but none of them are equivalent to the other choices. this is how i am doing it right now, but it’s not working very well:
while (choice1 == choice2 || choice1 == choice3 || choice1 == choice4)
choice1 = (rand() % max) + 1;
while (choice2 == choice1 || choice2 == choice3 || choice2 == choice4)
choice2 = (rand() % max) + 1;
while (choice3 == choice1 || choice3 == choice2 || choice3 == choice4)
choice3 = (rand() % max) + 1;
while (choice4 == choice1 || choice4 == choice2 || choice4 == choice3)
choice4 = (rand() % max) + 1;
does anyone else have a better way?
Since the number of elements is fixed and very small, your overall approach is pretty reasonable. However, the implementation is buggy.
Here is how it can be fixed:
There are better ways to do this if one needs a larger number of elements, or if the number of elements is variable.