虽然我写了srand(时间(0));它仍然产生相同的数字。还定义了ctime和cstdlib。有没有人帮我解决这个问题?感谢。
int main(void)
{
srand(time(0));
p_matrix_generate(10,3);
return 0;
}
我的p_matrix_generate函数:
void p_matrix_generate(int N, int zero_rate)
{
srand(time(0));
vector< vector<double> > p = create_2dvector(N, N);
int zero_number = ceil(pow(N, 2)*zero_rate / 100);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
p.at(i).at(j) = generateWeibull(1, 2)*((2 * generateRandomInteger(2, 5)) - 1);
cout << p.at(i).at(j) << " ";
}
cout << "\n";
}
}
威布尔函数:
double generateWeibull(int lower_bound, int upper_bound)
{
srand(time(0));
double temp;
default_random_engine generator;
weibull_distribution<double> distribution(lower_bound, upper_bound);
temp = distribution(generator);
return temp;
}
随机数函数:
int generateRandomInteger(int lower_bound, int upper_bound)
{
srand(time(0));
int random_integer = lower_bound + rand() % (upper_bound + 1);
return random_integer;
}