I am trying to generate a random number with rand as follows:
return a + ( rand( ) % n );
Where a is the shifting value (i.e., the first number in the desired range of consecutive integers) And n is the scaling factor (i.e,. the width of the desired range of consecutive integers).
-- C How to program 6th Edition - Deitel
I write it as:
return 1 + ( rand( ) % 1000 );
and it works, but when I write the code as follows:
return 1000 + ( rand( ) % 1112 );
I end up getting absurdly large numbers, for example 1756 and 1877. Those were the last two outputs that occurred.
I am returning the value as a integer number to a function call within a printf statement but I am doing the same with the working statement, so I do not think it the way I am calling the function.
What am I doing wrong...?