我试图在c ++中使用rand()函数,但每次都返回相同的随机数。
我已将声明srand(unsigned int(time(0)));
放在main函数的第一行,但我仍然遇到此问题。
我已经用Google搜索了问题,并在此网站上找到了几个答案,但他们都建议将srand()
语句放在main函数的第一行,我已经完成了。
有人可以帮我解决这个问题吗?感谢。
EDITED @ 5:10 pm:
该计划很复杂。我试图排除故障,这里有一些额外的信息:
主要功能:
int main(int argc, char **argv) {
srand(time(0));
std::auto_ptr<simulation> sim_ptr;
const double delta_t = 0.000002;
if (argc != 2) {
std::cerr << "Error: invalid number of arguments specified" << std::endl;
exit(EXIT_FAILURE);
} else if (strcmp(argv[1], "--random") == 0) {
sim_ptr = std::auto_ptr<simulation>(create_random_simulation(delta_t));
} else if (strcmp(argv[1], "--naive") == 0) {
const double alpha = 1000;
sim_ptr = std::auto_ptr<simulation>(create_naive_simulation(delta_t, alpha));
} else if (strcmp(argv[1], "--tree") == 0) {
const double alpha = 1000;
sim_ptr = std::auto_ptr<simulation>(create_tree_simulation(delta_t, alpha));
}
const size_t time_steps = 200;
const size_t particles_count = 2048;
const size_t particles_shown = 2048;
const size_t image_width = 512;
const size_t image_height = 512;
std::valarray<double> m = random_valarray(particles_count);
m = m * m * m;
std::valarray<double> x = random_valarray(particles_count);
std::valarray<double> y = random_valarray(particles_count);
std::valarray<double> vx, vy;
vx.resize(particles_count);
vy.resize(particles_count);
std::cout << "Simulation" << std::endl;
for (size_t time_step = 1; time_step <= time_steps; time_step++) {
std::cout << "\tFrame " << time_step << " / " << time_steps << std::endl;
sim_ptr->step(x, y, vx, vy, m);
std::vector<uint8_t> image;
image.resize(image_width * image_height);
for (size_t particle = 0; particle < particles_shown; particle++) {
const uint8_t color = ((particle * 23297) % 255) + 1;
draw_particle(x[particle], y[particle], m[particle], &image[0], image_width, image_height, color);
}
const std::string image_path = get_image_path(time_step);
write_bmp_image(image_path, &image[0], image_width, image_height);
}
return 0;
}
每次调用rand()的类函数为零:
void random_simulation::step(std::valarray<double>& x, std::valarray<double>& y,
std::valarray<double>& vx, std::valarray<double>& vy,
std::valarray<double>& m) const
{
const size_t particles_count = x.size();
assert(particles_count == y.size());
assert(particles_count == vx.size());
assert(particles_count == vy.size());
const double tau = 2.0 * sqrt(2.0 * this->delta_t);
for (size_t i = 0; i < particles_count; i++) {
x[i] = x[i] + tau * (((rand() % 2001 - 1000)/2000));
y[i] = y[i] + tau * (((rand() % 2001 - 1000)/2000));
if (i == 1){
std::cout << x[i] << " ";
std::cout << y[i] << " ";
}
}
}
simulation* create_random_simulation(double delta_t) {
return new random_simulation(delta_t);
}
根据循环,x[i]
和y[i]
应随每次迭代而变化,但每次迭代时它们保持不变。
答案 0 :(得分:3)
(rand() % 2001 - 1000)/2000
总是0
。您可以将2000.0
除以[0.0, 1.0)
您可以使用C ++ 11随机实用程序,例如std::uniform_real_distribution
:
std::default_random_engine generator(time(0));
std::uniform_real_distribution<double> distribution(0.0, 1.0);
double x = distribution(generator);
答案 1 :(得分:0)
我在XCode中运行了以下内容,每次都产生一个不同的随机数(使用C ++静态转换)
#include <iostream>
int main(int argc, const char * argv[])
{
srand(static_cast<unsigned int>(time(0)));
int r = rand();
std::cout << "Random number: " << r << std::endl;
return 0;
}
// Random numbers:
// 2064544746
// 2064897693
// 2065132991
// 2065250640
在i386 x86_64架构上使用Apple LLVM 4.2编译器
答案 2 :(得分:-1)
你是正确的,当使用rand()
时,必须在程序开头的某个时刻调用srand()
,以确保每次运行不同的伪随机数。但是,正确的用法是:
srand(time(NULL));
time(0)
返回0,每次运行时rand()
种子time(NULL)
,导致您的问题。 {{1}}返回内存中存储的时间点,并正确地为发生器播种。