Does vector calls multiple constructor or makes copies?

Hello all,
I've a class that generates object with some features: X-Y position, is_fellony, is_player..
I've different contructors:

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
 Elementi::Elementi(bool f, bool p, int max_dim){
	
	srand(time(NULL));
	
	x=rand()%max_dim;
	y=rand()%max_dim;
		
	fellony=f;
	if (fellony){is_player=false; symbol='X';}
	else {
		is_player=p;
			if(is_player) symbol='P';
			else symbol='O';
	}
	
	print_avvisoNew(x,y,symbol,fellony,is_player);
}

Elementi::Elementi(int new_x, int new_y, bool f, bool p, int max_dim){
	
	if(new_x <= max_dim) x=new_x;
	else x=max_dim;
	
	if(new_y <= max_dim) y=new_y;
	else y=max_dim;
		
	fellony=f;
	if (fellony==true){is_player=false; symbol='X';}
	else {
		is_player=p;
			if(!is_player) symbol='P';
			else symbol='O';
	}
	
	print_avvisoNew(x,y,symbol,fellony,is_player);	
}


I want to create n "enemies" (n chosen in run time) in a random position using first constructor (that has a rand() to initialize the xy position.
However, if I use both vector or pointers + new, it generates n object with same xy cohordinates (i.e. n copies of the same object).

1
2
3
4
	vector<Elementi> nemici;
	for(int i=0;i<num_nemici;i++){
		nemici.push_back(Elementi(1,0,max_dim)); //rand()%max_dim,rand()%max_dim,
	}



It generates different objects only in this way:
1
2
3
4
	vector<Elementi> nemici;
	for(int i=0;i<num_nemici;i++){
		nemici.push_back(Elementi(rand()%max_dim,rand()%max_dim,1,0,max_dim));
	}


Same story with pointers.

Does anyone know why do I create copies and not different object?

Thanks!!
Riccardo
The time function returns the time in seconds so it is likely that you are passing the same seed to srand each time. The same seed means you'll get the same sequence of "random" numbers from rand(), which explains why all your elements get the same set of coordinates.

Normally srand should only be called once in a program, before the first call to rand(). At the beginning of main() is usually a good place to call it.
Last edited on
Every time you invoke the Elementi::Elementi(bool f, bool p, int max_dim) constructor, you seed the random number generator with the current time, in seconds. This means that, if you invoke the constructor multiple times within the same second, you re-seed the RNG with the same number - and, therefore, get the same "random" numbers.

In your final code example, you're not re-seeding the RNG, so you get different random numbers on each iteration of the loop.

You should seed the RNG once, at the start of your program, not over and over.
Last edited on
copies are different objects.

Your problem with the same x/y coordinates stems from the wrong use of srand(time(NULL));. Within the same second it will have the same seed and hence rand() will produce the same numbers. To produce different numbers per call of rand() you should move srand(...) to the top of main and call it just once.
Wow - a double-ninja :)
Topic archived. No new replies allowed.