Mail Archives: djgpp/1999/07/24/18:53:47
> You can use the remainder operator like this:
>
> number = (rand() % 101) + 1;
The rand() % 101 part will return a number from 0 to 100, so if you add
one to that you get 1 to 101. Try (rand() % 100) + 1 instead (1 to
100).
> This will generate a number from 1 to 100. You can also substitute rand()
> with random() which generates numbers that are more random, but random()
> isn't as portable. Don't forget to seed it at the start of your program like
> this:
>
> srand(time(0)); /* for rand() */
> srandom(time(0)); /* for random() */
- Raw text -