Because, on your platform, RAND_MAX == INT_MAX
.
The expression range*rand()
can never take on a value greater than INT_MAX
. If the mathematical expression is greater than INT_MAX
, then integer overflow reduces it to a number between INT_MIN
and INT_MAX
. Dividing that by RAND_MAX
will always yield zero.
Try this expression:
random_integer = lowest+int(range*(rand()/(RAND_MAX + 1.0)))