samedi 18 mars 2017

rand() giving same number and it's out of the range

I'm trying to fill a 2D array with random numbers, but every time I run the program it gives me the same huge negative number that isn't even in my range. I looked everywhere and couldn't find an answer. Please help!

Here's my function:

void fill_random(int matrix[10][10], int row_size, int col_size)
{
    for (int i = 0; i < row_size; i++)
        for (int j = 0; j < col_size; j++)
            matrix[i][j] = -10 + (rand() % 21);
}

I have srand((int)(time(0))); in my main. The function keeps giving me the number -858993460 every time. My range is supposed to be [-10,10].




Aucun commentaire:

Enregistrer un commentaire