【发布时间】:2020-07-12 07:10:10
【问题描述】:
我正在尝试生成一个介于 0 和数组大小之间的数字。 有我的代码,但输出始终为 2。 编辑:我尝试使用另一个编译器,输出只有 7 请帮帮我。
这是我的完整代码
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
int main (){
string motMystere = ("Bonjour");
int tailleMotMystere (0);
vector<string> motMelange;
tailleMotMystere= motMystere.size();
srand(time(0));
int nombreRandom = 0;
nombreRandom = rand() % tailleMotMystere;
cout << motMystere.size() << endl;
return 0;
}
【问题讨论】:
-
即使这个数字链也可以是随机的:2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 ,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2。也许你只是运气不好
-
因为它被标记为 C++,我建议你考虑使用 C++ random features
-
您是否在每次拨打
rand之前都拨打srand?我不知道,因为这不是minimal reproducible example。如果你是,你不应该。 -
@c_kusanagi 能否请您提供整个代码(如果您正在循环并调用 rand(),请也包括在内。)
-
根据您提供的代码,您不应该得到重复的结果。但是,如果您将它们放入某种循环中,则可能是您的循环结构错误。