Manipulating what rand() returns
Since rand() returns unsigned integers in a large range, we often have to manipulate the return value to suit our purposes
Suppose we want only random numbers in the range from 0 to 5
num = rand () % 6
How about 1 to 6?
num = 1 + rand( ) % 6;
How about 5 to 20?
num = 5 + rand ( ) % 16;
Previous slide
Next slide
Back to first slide
View graphic version