lundi 28 juin 2021

why using rand() without setting seed with srand() some numbers change?

I'm generating random numbers with rand() bounded to 1000 with this little piece of code:

for(int i=0;i<pARRAYSIZE;i++) pARRAY[i] = rand()%MAX_VALUE_DATA;

(I'm NOT using srand() function, so by default seed would be 1? [1])

Sometimes I get this sequence:

362 , 27 , 690 , 59 , 763 , 926 , 540 , 426 , 172 , 736 , 211 , 368 , 567 , 429 , 782 , 530 , 862 , 123 , 67 , 135 , 929 , 802 , 22 , 58 , 69 , 167 , 393 , 456 , 11 , 42 , 229 , 373 , 421 , 919 , 784 , 537 , 198 , 324 , 315 , 370 , 413 , 526 , 91 , 980 , 956 , 873 , 862 , 170 , 996 , 281 , 305 , 925 , 84 , 327 , 336 , 505 , 846 , 729 , 313 , 857 , 124 , 895 , 582 , 545 , ...

And sometimes I get this other sequence:

27 , 690 , 59 , 763 , 926 , 540 , 426 , 172 , 736 , 211 , 368 , 567 , 429 , 782 , 530 , 862 , 123 , 67 , 135 , 929 , 802 , 22 , 58 , 69 , 167 , 393 , 456 , 11 , 42 , 229 , 373 , 421 , 919 , 784 , 537 , 198 , 324 , 315 , 370 , 413 , 526 , 91 , 980 , 956 , 873 , 862 , 170 , 996 , 281 , 305 , 925 , 84 , 327 , 336 , 505 , 846 , 729 , 313 , 857 , 124 , 895 , 582 , 545 , 814 , ...

Which is the same as the first one but with 362 at the start (the real squence have a size of 1024 numbers, when 362 don't exist it generates a 871 in a fixed position of the array to complete the 1024 numbers).

And in a relly few cases it generates this other sequence which is the second one without 27, 690 and 59:

763 , 926 , 540 , 426 , 172 , 736 , 211 , 368 , 567 , 429 , 782 , 530 , 862 , 123 , 67 , 135 , 929 , 802 , 22 , 58 , 69 , 167 , 393 , 456 , 11 , 42 , 229 , 373 , 421 , 919 , 784 , 537 , 198 , 324 , 315 , 370 , 413 , 526 , 91 , 980 , 956 , 873 , 862 , 170 , 996 , 281 , 305 , 925 , 84 , 327 , 336 , 505 , 846 , 729 , 313 , 857 , 124 , 895 , 582 , 545 , 814 , 367 , 434 , 364 , ...

[1] Some test I did: if I force the seed to be 1 with srand(1) I always get the following sequence:

383 , 886 , 777 , 915 , 793 , 335 , 386 , 492 , 649 , 421 , 362 , 27 , 690 , 59 , 763 , 926 , 540 , 426 , 172 , 736 , 211 , 368 , 567 , 429 , 782 , 530 , 862 , 123 , 67 , 135 , 929 , 802 , 22 , 58 , 69 , 167 , 393 , 456 , 11 , 42 , 229 , 373 , 421 , 919 , 784 , 537 , 198 , 324 , 315 , 370 , 413 , 526 , 91 , 980 , 956 , 873 , 862 , 170 , 996 , 281 , 305 , 925 , 84 , 327 , ...

Edit: without using srand() I got a new sequence, which is similar to the second one but without 27...

690 , 59 , 763 , 926 , 540 , 426 , 172 , 736 , 211 , 368 , 567 , 429 , 782 , 530 , 862 , 123 , 67 , 135 , 929 , 802 , 22 , 58 , 69 , 167 , 393 , 456 , 11 , 42 , 229 , 373 , 421 , 919 , 784 , 537 , 198 , 324 , 315 , 370 , 413 , 526 , 91 , 980 , 956 , 873 , 862 , 170 , 996 , 281 , 305 , 925 , 84 , 327 , 336 , 505 , 846 , 729 , 313 , 857 , 124 , 895 , 582 , 545 , 814 , 367 , ...

Code:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

void initVector(int *pARRAY, int pARRAYSIZE) {
    for(int i=0;i<pARRAYSIZE;i++) pARRAY[i] = rand()%1000;
}

int main(int argc, char** argv){
    int rank, nrProcesos;

    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&nrProcesos); 

    int *E = (int*) malloc(sizeof(int) * 1024);

    if (rank==0)
    {
        //srand(1);
        initVector(E, 1024);
    }

    #ifdef DEBUG
        if(rank == 0){
            for(int i=0;i<1024;i++){
                if(i%8==0) printf("\n");
                printf("%-4d, ", E[i]);
            } printf("\n");
        }
    #endif //DEBUG

    MPI_Finalize(); 
    return(0); 
}

mpicc -DDEBUG -o stackoverflow stackoverflow.c mpirun --bind-to none -np 4 stackoverflow




Aucun commentaire:

Enregistrer un commentaire