Can anyone tell me why this function only ever outputs a maximum of 16? c++ -


srand((int)time(nullptr));  (int = 1; <= 10000000; i++) {     int level = 1;     int _rand = rand() % 2;     while (_rand)     {         level++;         _rand = rand() % 2;     }      if (level > 15) {        cout << level << " ";     } } 

this code acts coin toss. testing how many concecutive "heads" can generate seems 15 can manage (+1 level assigned). can run millions of times , generate hundreds/thousands of 16s never higher.

http://www.cplusplus.com/reference/cstdlib/rand/ says

"

v1 = rand() % 100;         // v1 in range 0 99 v2 = rand() % 100 + 1;     // v2 in range 1 100 v3 = rand() % 30 + 1985;   // v3 in range 1985-2014  

notice though modulo operation not generate uniformly distributed random numbers in span (since in cases operation makes lower numbers more likely). "

maybe more advanced pseudo-random generator give better luck


Comments