random numbers

Hello, I wrote a recursive function and I don't know if it is the cause that the random naumers seems don't work. That is to say it works one time but as they are in a loop they expect to change more times
thanks


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
....
main method here
  for(int n=0; n<5; n++)
	{
		formula = setFormula(individuo, n);
	}
	cout << "formula:"<<formula<<endl;
	return 0;
}
	//Formula recursiva
string setFormula(vector<Individuo> ind, int n)
{
	unsigned int seed{0};
	srand(static_cast<unsigned int>(time(0)));
	int i = rand()%ind.size();	
	//int j = rand()%operacion.size();
	cout<<"i="<<i<<endl;
	if (n<=1)
	{
		return ind[i].getFactor();
	}
	else
	{
		return "("+ind[i].getFactor()+ind[i].getOperacion() + setFormula(ind,n-1)+")";
	}

> unsigned int seed{0};
> srand(static_cast<unsigned int>(time(0)));
Do this once, in main()

Calling it in a loop, where time(0) is effectively a constant, just means rand() is always going to give you the same number.
Topic archived. No new replies allowed.