Solution for Exercise 11-18

ex1118

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int r,a,b;

    puts("100 Random Numbers");
    for(a=0;a<20;a++)
    {
        for(b=0;b<5;b++)
        {
            r=rand();
            r%=21;
            printf("%d\t",r);
        }
        putchar('\n');
    }
    return(0);
}

Notes

* The result of the program is to display 100 random numbers in the range of 0 through 20.

* The zero is possible because r%=21 generates a value of zero when the random number coughed up by the rand() function is divisible by 21.

* You could also combine Lines 13 and 14 to read:

* Most of the random number generating routine you'll use incorporate the modulus operator to curb the values, just as shown above.