rand() gives the same number through out my program

I have a Task class which looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;

class Task
{
private:
	string name;
	int time_of_completion;
public:
	Task();
};


and here is the default constructor
1
2
3
4
5
6
Task::Task()
{
	srand(time(NULL));
	name = "";
	time_of_completion = (rand()%10 + 1)*10;
}


My problem is that whenever I initialize a task in my program, it gets the same number for time_of_completion again and again. Each program run gets a new random number for time_of_completion but that number will say the same through out each run.
Example: if I have Task a,b,c; in my main program then a,b,c will have the same time_of_completion no matter what.
Can you please help me with how to make each new task will be initialized with a new random time_of_completion.
Thank you for your time and patience
Remove the call to srand() from the constructor.

Call srand() once, in main(), at the start of your program.
Thank you very much, my program now works perfectly.
Did a little research and found out that if I put the srand(time(NULL)) in the constructor it'll be called many times at the same second and gives me the same value for rand()
Topic archived. No new replies allowed.