lundi 13 mars 2017

rand() produces 1 number too high?

It is my understanding that rand() % a + b will return numbers between a and b including both a and b. I must be misunderstanding this though, because when I run the code below int r will return 2, 3, or 4. I, of course, am expecting it to return 2 or 3. I'm calling srand(time(NULL)); in main and I'm using #include <time.h> and #include <stdlib.h>
Am I missing something?

    int r = (rand() % 3) + 2;
    if (r ==2)
        g_fan.draw(r); // skin == 2
    else
        g_fan.draw(1 + r); //skin == 4




Aucun commentaire:

Enregistrer un commentaire