Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 生成唯一的多个随机数_C++_Arrays_Qt_Random - Fatal编程技术网

C++ 生成唯一的多个随机数

C++ 生成唯一的多个随机数,c++,arrays,qt,random,C++,Arrays,Qt,Random,我想生成唯一的随机数,并在这些随机数的函数中添加项。这是我的密码: 问题是当我验证生成的数字是否存在于带有代码结果的数组中时。contains(randomNb): int nbRandom=ui->randoomNumberSpinBox->value(); //nbRandom是我们想要的随机数的数目 int i=1; int结果[1000]; 而(iresultsListWidget->addItem(pepoles[randomNb]); 结果[i]=nB; //我们在数组中添加新的ra

我想生成唯一的随机数,并在这些随机数的函数中添加项。这是我的密码:

问题是当我验证生成的数字是否存在于带有代码
结果的数组中时。contains(randomNb)

int nbRandom=ui->randoomNumberSpinBox->value();
//nbRandom是我们想要的随机数的数目
int i=1;
int结果[1000];
而(iresultsListWidget->addItem(pepoles[randomNb]);
结果[i]=nB;
//我们在数组中添加新的randomNb
i++;
}
}

结果
是一个数组。这是内置C++类型。它不是类类型,也没有方法。所以这是行不通的:

results.contains(randomNb)
您可能希望使用QList来代替。比如:

QList<int> results;
更改后,您的代码将变成:

int nbRandom = ui->randoomNumberSpinBox->value();
//nbRandom is the number of the random numbers we want
int i = 0;
QList<int> results;
while ( i < nbRandom ){
    int randomNb = qrand() % ((nbPepoles + 1) - 1) + 1;
    if(!results.contains(randomNb)){
        //if randomNb generated is not in the array...
        ui->resultsListWidget->addItem(pepoles[randomNb]);
        results << randomNb;
        //We add the new randomNb in the array
        i++;
    }
}
int nbRandom=ui->randoomNumberSpinBox->value();
//nbRandom是我们想要的随机数的数目
int i=0;
QList结果;
而(iresultsListWidget->addItem(pepoles[randomNb]);

结果…你的问题是…什么?你似乎离找到一个有效的解决方案还有一步之遥。你所需要的只是一个函数,它检查数组中是否有特定的数字(特定大小)。然后你可以替换
results.contains(randomNb)
调用该函数。您有什么理由不能自己编写该函数吗?这就是您寻求帮助的原因吗?哦,对不起,我已经编辑了我的问题^
结果。包含(randomNb)<代码>不是合法的C++。它不能是你的代码。它是不可行的,这是问题!为什么我应该正确?抱歉,但是我在QT和C++中是非常坏的,你能在我的代码中写这个吗?谢谢!@罗切斯特福克斯也注意到你的代码中的一个错误。我又更新了答案。读一下文档。如果你弄不懂,发布一个新的曲。估计。虽然不是很难。只要花点时间:-)
results << randomNb;
int i = 0;
int nbRandom = ui->randoomNumberSpinBox->value();
//nbRandom is the number of the random numbers we want
int i = 0;
QList<int> results;
while ( i < nbRandom ){
    int randomNb = qrand() % ((nbPepoles + 1) - 1) + 1;
    if(!results.contains(randomNb)){
        //if randomNb generated is not in the array...
        ui->resultsListWidget->addItem(pepoles[randomNb]);
        results << randomNb;
        //We add the new randomNb in the array
        i++;
    }
}