Fill array with random numbers

I have an exercise where I'm required to create an array of any size and fill it with random numbers. There's something in this strain of thought that is wrong:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void randomArrayFill(int* arrayFill, int size)
{
	for (int i = 0;i < size ; i++)
	{
		arrayFill[i]=getRandomNumber(); 
	}
}

int getRandomNumber()
{
	int randomNumber = 0; 
	
	srand(time(0)); //Seed the random system

	randomNumber = rand() % 1500; //keep random numbers under 1500

	return randomNumber;
}


The output is an array filled with the same random number. eg array of 5:{20,20,20,20,20} instead of being filled with a complete set of different random numbers.

Can somebody explain to me why this happens ?
You should place this statement

srand(time(0)); //Seed the random system

outside the body of the function

The problem is that the time does not changed in this period due to a high speed of the computer. So the result of the function srand is always the same.
You need only once call it and you will get a unique sequence of random numbers.
Last edited on
Thanks for the quick reply, it worked like a charm.

many thanks
Topic archived. No new replies allowed.