c++ - better way of making sure 4 random values are not equivalent at all? -
c++ - better way of making sure 4 random values are not equivalent at all? -
i have qt form has multiplication problem , 4 buttons. 4 buttons (choices) randomly generated , question.
i want randomize 4 choices random none of them equivalent other choices. how doing right now, it's not working 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 else have improve way?
since number of elements fixed , small, overall approach pretty reasonable. however, implementation buggy.
here how can fixed:
choice1 = (rand() % max) + 1; { choice2 = (rand() % max) + 1; } while (choice2 == choice1); { choice3 = (rand() % max) + 1; } while (choice3 == choice1 || choice3 == choice2); { choice4 = (rand() % max) + 1; } while (choice4 == choice1 || choice4 == choice2 || choice4 == choice3);
there improve ways if 1 needs larger number of elements, or if number of elements variable.
c++
Comments
Post a Comment