supriya98 supriya98 - 1 year ago 55
C++ Question

What does " rand()%3-1 " mean in c++?

The program creates a two-dimensional array (named "table" and fills the array with random numbers (-1, 0 or 1).

int main() {
srand(time(0));
const int ROWS=3;
const int COLS=4;

int table[ROWS][COLS];

for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
table[i][j] = rand()%3-1;
}
}

for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++)
cout << setw(3) << table[i][j];

cout << endl;
}

return 0;
}

Answer Source

rand() % 3 give a number in the range 0 --> 3

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download