Why does this code from her tutorial only print out negative numbers? I thought it was supposed to print out numbers between 1-10.
I don't understand how (double)std::rand() / ( RAND_MAX + 1 ) could ever be a negative number, unless rand() is generating one (which would seem a tad odd). Or maybe it's (RAND_MAX + 1) that is somehow overflowing to -RAND_MAX?Code:#include <iostream> #include <cstdlib> #include <ctime> int main() { std::srand ( (unsigned int)std::time ( 0 ) ); std::cout<< (int)( (double)std::rand() / ( RAND_MAX + 1 ) * 10 ) <<std::endl; return 0; }
EDIT: It is indeed (RAND_MAX+1) that is overflowing.