Producer and Consumer problem

Hey guys I am working on a producer and consumer problem where a producer will produce 5 random numbers and put it on a global array and a seperate consumer function will the numbers of the global array and print them. I am having a problem with putting the elements on the global array because everytime I run it, it inserts the same numbers continuously. I am hoping someone can help me fix it so each random number is different. Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//MAT375 Buffer Project

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int num=0;
int a[5];

void producer()
{
	srand((unsigned)time(0)); 
	a[num]=rand();
	cout << "The producer put " << a[num] << " into the buffer." << endl;
}
void consumer()
{
	cout << "The consumer got " << a[num] << " from the buffer." << endl;
	num++;
}

int main()
{
	for(int i=0;i<5;i++)
	{
		producer();
		consumer();
		num++;
	}
	system("pause");
	return 0;
}

Call srand(...) once at the beginning of main() instead of inside producer().
It worked! Thanks for your help.
Topic archived. No new replies allowed.