The reason why you're not getting the full possible range is because rand()
is ancient.
Back in the days when rand()
was developed, the common way to implement a random-number generator was a so-called Linear Congruential Generator (lcg). This doesn't give very random numbers; in fact they're quite predictable if you've seen the first few. But computers back then were slow and worked on small numbers.
That's why rand()
only promises 15 bits - the constant RAND_MAX
defines the maximum value you can get from RAND
, and it can be as low as 32767
. 32 bit computers back then were not so common, so using 16 bits numbers made sense. And with common LCG implementations, the lowest bit was worthless (toggled between 0 and 1) so you wanted to discard that anyway.
Several decades of improvement have resulted in much better generators. In particular, the mt19937
generator is very good and still very efficient. Since 2011, C++ has <random>
and it includes std::mt19937
. Also, for convenience you can now pick your own distribution, with std::uniform_int_distribution<>(min,max)