Dear all,
I have the following C++ code that tried to generate a random number. The idea is we given some rate "x" and number of runs; we hope it would generate the number as many as (x * number of runs times).
#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;
}
Hence if we run the code above K times with this command:
$ a=0 ; while test $a -lt 10 ; do ./MyCode | wc -l ; a=`expr $a + 1` ; done
We expect it to generate number "3" as many as ~3000 times in 1M runs. But some how my code above my code above only generate number "3" as many as 900 ~ 1000 times.
How can I improve on my code above?