更好的方法是确保4个随机值根本不相等?

use*_*094 1 c++

我有一个qt表单,有一个乘法问题和4个按钮.4个按钮(选项)是随机生成的,问题也是如此.

我只想将4个选项随机化,因此它们是随机的,但它们都不等同于其他选择.这就是我现在正在做的事情,但是效果并不好:

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;
Run Code Online (Sandbox Code Playgroud)

还有其他人有更好的方法吗?

Pla*_*aHH 10

像(未经测试,只是写在这里,但你应该得到的想法):

std::set<int> s;

while(s.size() < 4 )
{
    s.insert(rand());
}
Run Code Online (Sandbox Code Playgroud)