vendredi 25 octobre 2019

What is the range that the random function can return?

int nextInt(int n) {
    return rand() / static_cast<double>(RAND_MAX) * n;
}

I find someone write the above function(nextInt) and he said nextInt returns a random int in [0, n - 1], but I think nextInt returns a random int in [0, n]. I wonder who is right and why?




Aucun commentaire:

Enregistrer un commentaire