You say in a comment that if you call srand()
before rand()
, your compiler doesn't accept it.
That's because your compiler (Microsoft?) is enforcing C89/C90 rules, which don't permit declarations to follow statements within a block.
You can work around that limitation by adding a new block. A rough outline:
srand(time(NULL));
{
int size = rand() % 100 + 1;
/* ... */
}
Or you can remove the initializers for size
and array2
, replacing them with assignments after calling srand()
. Personally, I prefer to initialize variables as early as possible.
Generally speaking, you should call srand()
exactly once in your program, before any calls to rand()
. (That's for the most general case where you want non-repeating pseudo-random numbers; if you actually need a repeatable sequence, a different strategy is appropriate.)
(Without a call to srand()
, rand()
behaves as if you had called srand(1)
, and generates the same sequence each time.)
(I've changed the name of your variable SIZE
to size
; all-caps identifiers are conventionally used for macros.)