mardi 5 novembre 2019

Why does rand() produce random number higher than scaling factor?

I am trying to get a random number up to a maximum of the scaling factor used, which is 1112, and at a minimum of 1000, which is the shifting value I'm using. However, when I run the program, the output I get is 1127. What am I doing wrong? Thank you.

Here is the code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int n = 0;
    n = 1000 + rand() % 1112;
    cout << n << endl;
}

Here's my output in terminal:

1127
Program ended with exit code: 0



Aucun commentaire:

Enregistrer un commentaire