// Warning Warning Warning // This shows how bad random number result, // if you re-seed your generator // g++ -std=c++0x main_badrandom.cpp #include #include #include #include #include using namespace std; std::mt19937 gener; // define generator double bad_unirand(void){ static uniform_real_distribution unif_dist(0,1); return (unif_dist(gener)); } int main(){ ofstream outunif("badunif"); outunif << std::fixed< (std::time(0))); // seed for the generator cout<<"Bad uniform distribution to file badunif \n"; for(int i = 0; i < 100000; i++) { // re-seed generator after every 10000 numbers if(i%10000==0) {gener.seed(static_cast (std::time(0)));}; outunif << bad_unirand() << "\n"; } outunif.close(); cout<<"If you look at the contents of file badunif, you probably find nothing wrong\n"; cout<<"Now plot the data.\nFor example in gnuplot, type\n plot 'badunif' w d\n"; cout<<"See the non-randomness?\n"; return 0; }