0

I create Array with size N in cycle "for" and fill up random numbers.I have startPoint which means first size of array. endPoint it's end size, but step is step of size :)

for(int n = startPoint; n <= endPoint; n += step)
{
    int * Array = malloc(sizeof(int)*n);//Create dynamic array wit size n

    generationNumber(Array,n);// Function which generate random numbers and write to array

    printArray(Array,n);//Here I print array to cmd

    Executing(Array,n);//Here I execute some action with array, it's not important  

    free(Array);
}

So my problem is in results: enter image description here

It's a function generationNumber:

void generationNumber(int * Array, int n)
{
     srand((unsigned)time(NULL));

     for(int i = 0; i < n; i++)
     {
        Array[i] = rand()%1000;
     }

}

Why numbers are repeated? I feel that just add numbers to array.

dtell
  • 2,488
  • 1
  • 14
  • 29
White User
  • 33
  • 7

1 Answers1

1

srand((unsigned)time(NULL)); will seed rand() with the current time in seconds. Running srand((unsigned)time(NULL)); more than once in a second will reset its state.

N00byEdge
  • 1,106
  • 7
  • 18