我有以下C++代码试图生成 的随机数。我们给出了idea一些速率“x”和运行次数;我们希望它会生成多达(x *运行次数)的数量。以一定的速率生成随机数
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <time.h>
using namespace std;
int main () {
// Initialize Random Seed
srand (time(NULL));
string line;
double SubsRate = 0.003;
double nofRuns = 1000000;
for (unsigned i=0; i < nofRuns ; i++) {
int toSub = rand() % 1000 + 1;
if (toSub == (SubsRate * 1000)) {
cout << toSub << " Sub" << endl;
}
}
return 0;
}
因此,如果我们用这个命令运行上述K次代码:
$ a=0 ; while test $a -lt 10 ; do ./MyCode | wc -l ; a=`expr $a + 1` ; done
我们期望它在1M运行生成号码“3”多达〜3000倍。但 一些如何我的代码以上我的代码上面只生成多达900〜1000倍的数字“3”。
我该如何改进上面的代码?
你说得对。我没有实现他的+1。 – 2009-03-05 17:29:29