0

I have a big problem when I try to compare two matrix. Every time when I run the program it prints and sets the two matrix with the same values. However you can see in the code below, I have put 2 different matrix with random numbers but It prints always the same numbers in the two matrix... Where is the fail?

#include <stdio.h>
#include <time.h>

void further(int matrix[][3]);
void check(int mat[][3], int another[][3]);

int main (){
    int mat[3][3];
    int another[3][3];

    further(mat);
    further(another);
    check(mat,another);

    system("pause");
    return 0;
}

void further(int matrix[][3]){
    srand(time(NULL));
    int i,j,aux;
    for(i=0; i<3; i++){
        for(j=0; j<3;j++){
            aux=rand()%10;
            matrix[i][j]=aux;
        }
    }
 }

void check(int mat[][3], int another[][3]){
    int i,j,aux;
    aux = 0;
    for(i=0; i<3 && aux == 0; i++){
        for(j=0; j<3 && aux == 0; j++){
            if(mat[i][j] != another[i][j]){
                aux = 1;
            }
        }
    }
    for(i=0; i<3; i++){
        for(j=0; j<3; j++){
            printf("%i ",mat[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    for(i=0; i<3; i++){
        for(j=0; j<3; j++){
            printf("%i ",another[i][j]);
        }
        printf("\n");
    }
    if(aux==0){
        printf("Those matrix are equal.\n\n");
    }
    else{
        printf("Those matrix are NOT equal.\n\n");
    }
}
alk
  • 69,737
  • 10
  • 105
  • 255
Bechma
  • 425
  • 6
  • 11
  • I guess user3121023 is right. I think that you get the same sequence of random numbers both times (because rand() is a pseudo random generator and you might be initializing it with the same seed in `srand(time(NULL))`) – GKalnytskyi Jan 10 '15 at 14:29
  • OT: Also be aware that `time_t` not necessarily needs to be an integer. It might very well be a pointer or a structure or ... On POSUX systems however `time_t`is guaranteed to be an arithmetic type. – alk Jan 10 '15 at 14:34

3 Answers3

2

As @user3121023 said in question's comments, you need to move srand(time(NULL)); to your main function.

You can find a pretty good explanation of why it's necessary here; the point is the following:

Seed is usually taken from the current time, which are the seconds, as in time(NULL), so if you always set the seed before taking the random number, you will get the same number as long as you call the srand/rand combo multiple times in the same second.

Hope this helps.

Community
  • 1
  • 1
Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24
1

The numbers returned by rand are not random, they are 'pseudo-random'. rand is deterministic. To make pseudo-random numbers to be different for different runs of program, the pseudo-random generator (rand) is usually initialized by some 'random' number (e.g. current time or PID), this is what srand does. If you initialize the generator with the same number, rand will produce the same sequence.

Now, your program is running fast, and both calls to srand(time) are using essentially the same time (the clock does not tick between calls to further). So in both calls the pseudo-random generator starts from the same seed, and you are filling matrices with the same values.

Move the pseudo-random generator initialization (srand) into main, so it would be called only once, and see the difference.

nullptr
  • 11,008
  • 1
  • 23
  • 18
0

As you know, srand is used to seed the rand pseudo-random number generator, and if you use the same seed twice, you will get the exact same series of random numbers.

The problem here is that you seed rand with srand(time(NULL)) before each matrix, and time returns a number of seconds. So if there is less than one second between those two calls to srand you will have the same seed, and generate the same numbers.

You should move that srand call to main and remove it from further, or make sure you don't call it twice within the same second.

Here is how those two functions would look like after the change :

int main (){
    srand(time(NULL));

    int mat[3][3];
    int another[3][3];

    further(mat);
    further(another);
    check(mat,another);

    system("pause");
    return 0;
}

void further(int matrix[][3]){
    int i,j,aux;
    for(i=0; i<3; i++){
        for(j=0; j<3;j++){
            aux=rand()%10;
            matrix[i][j]=aux;
        }
    }
 }

As a rule of thumb, a single call to srand in main before doing anything should avoid all those problems.

tux3
  • 7,171
  • 6
  • 39
  • 51