“rand()%3-1”在 C++ 中是什么意思?

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

程序创建一个二维数组(名为"table")并用随机数(-1、0 或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;
}

rand() % 3 给出 0 --> 2

范围内的数字