我目前在生成-32.768到32.768之间的随机数方面遇到了麻烦。它总是给我相同的值,但小数点后面有微小变化,例如:27.xxx。
这是我的代码,请帮忙看看。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand( time(NULL) );
double r = (68.556*rand()/RAND_MAX - 32.768);
cout << r << endl;
return 0;
}
srand
通常不会导致从第一次调用rand
开始就得到相邻的结果。(虽然由于对rand
质量没有实际要求,你的体验可能会有所不同。) - aschepler